ก่อนหน้านี้เราเคยพูดถึงการใช้ Wait แทนการใช้ Sleep เพื่อลดระยะเวลาการรันไปบ้างแล้ว (ใครพลาดไป Link นี้ครับ) แต่เจ้า Selenium เองก็มีระบบการอการแสดงผลของ Element มากกว่าแค่ wait เพียงอย่างเดียวเป็นยังไงมาดูกันครับ
การรอหลักๆของ Robot framework Selenium นั้นแบ่งเป็น 2 ประเภทหลักๆคือ Timeout และ Implicit wait
Timeout คืออะไร
เป็น Wait ที่จะถูกใช้ก็ต่อเมื่อเราสั่ง Wait …. อะไรสักอย่างเช่น Wait Until Element Is Visible โดยคำสั่งนั้นก็จะรอจนกว่า Element จะแสดง ภายในระยะเวลาที่กำหนด
การกำหนดเวลาการรอทำได้ 2 ส่วน หลักๆ คือ
แบบแรก กำหนด Global สามารถทำได้ตอนที่เรา import selenium library มาเลย โดยค่าที่เรากำหนัดจะถูกใช้เป็นค่า default ของ keyword Wait ทั้งหมดนั่นเอง
ตัวอย่าง การกำหนด Default Explicit wait ผ่านทาง Timeout parameter
1 2 |
*** Settings *** Library SeleniumLibrary timeout=10 |
แบบเฉพาะครั้ง สามารถส่งเป็น parameter ของ keyword wait ได้เลย โดยจะมีผลเฉพาะครั้งนั้นเท่านั้น
1 |
Wait Until Element Is Visible css=input[aria-label="Search input 1"] timeout=10s |
ตัวอย่าง Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
*** Settings *** Library SeleniumLibrary timeout=5.0 *** Test Cases *** Demo Selenium timeout [Teardown] Close All Browsers Open Browser https://ajaxsearchpro.com/ browser=chrome Maximize Browser Window Wait Until Element Is Visible css=input[aria-label="Search input 1"] Click Element css=input[aria-label="Search input 1"] Input Text css=input[aria-label="Search input 1"] cars # This wait will use default wait time equal to 5 seconds that we define when import library Wait Until Page Contains Lamborghini Urus |
Implicit wait คืออะไร
เป็น Wait ที่จะถูกใช้งานแบบ อัตโนมัติ โดยจะถูกใช้งานในทุกครั้งที่เราสั่งกดปุ่ม หรือค้นหา element ต่างๆบนหน้าจอ Default จะถูกตั้งไว้ที่ 0 seconds หรือไม่ถูกเปิดใช้นั่นเอง
การเปิดใช้งาน สามารถเปิดได้จากการ import library และกำหนดค่า implicit wait ได้ตามนี้ครับ
1 2 |
*** Settings *** Library SeleniumLibrary implicit_wait=2 |
เมื่อรากำหนดตามนี้แล้ว ทุกๆคำสั่งของ Selenium ที่มีการค้นหา Element จะถูกทำให้มีการรอสูงสุด 2 วินาที แบบอัตโนมัติ
ตัวอย่าง Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
*** Settings *** Library SeleniumLibrary timeout=5.0 *** Test Cases *** Demo Selenium timeout [Teardown] Close All Browsers Open Browser https://ajaxsearchpro.com/ browser=chrome Maximize Browser Window Wait Until Element Is Visible css=input[aria-label="Search input 1"] Click Element css=input[aria-label="Search input 1"] Input Text css=input[aria-label="Search input 1"] cars # This wait will use default wait time equal to 5 seconds that we define when import library Wait Until Page Contains Lamborghini Urus |
ฟังแล้วดูเจ้า Implicit wait จะเหมือนพระเอกขึ่ม้าขาวเลยใช่ไหมครับ แต่การใช้งาน Implicit wait ก็มีข้อเสียเหมือนกัน เช่น จะทำให้ step ช้าลง เช่น หากเรามีกรณีที่ต้องเช็ค UI ที่แสดงผลแบบ random การใช้ implicit wait จะทำให้การเช็ครอนานขึ้นอีกครับ เพราะ implicit wait จะวนหาจนกว่าจะ timeout นั่นเอง
ดังนั้นไม่ควร ตั้ง implicit wait >= Timeout ครับ และควรตั้งค่า implicit wait ไม่ให้สูงเกินไปครับ เพื่อไว้แก้เรื่อง animation ต่างๆของ application พอครับ ส่วนการ Wait ให้ data โหลด ควรจะ handle ด้วย Timำout มากกว่าครับ
Download: example source code