隱式等待執行什麼操作?
隱式是執行中每一步測試的預設等待時間。因此,如果我們設定十秒鐘的隱式等待,則每一步測試都將等待指定時間,然後採取行動並進入下一步。
隱式等待是一種動態等待,這意味著如果等待時間為十秒鐘,而下一個操作將作用於第五秒鐘的 Web 元素,則控制元件將立即進入下一步測試,而無需等待滿十秒鐘。
然而,如果元素在第十秒鐘之前不可用,則會引發異常。隱式等待簡單易用,但有一些缺點,如下所示 −
減慢測試執行時間。
無法捕捉程式碼中的效能錯誤。
隱式等待應用於參與測試執行的所有元素,且被視為全域性等待。
示例
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class Implictwt { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://tutorialspoint.tw/index.htm"; driver.get(url); //implicit wait with time in seconds applied to each elements driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); //Using id tagname attribute combination for css expression driver.findElement(By.cssSelector("input[name=’search’]")). sendKeys("Selenium"); driver.close(); } }
廣告