Selenium webdriver explicit wait

后端 未结 7 1454
无人共我
无人共我 2021-01-03 10:21

I\'m writing some automated tests for using the selenium chrome driver. I trying to write a reusable method that will explicitly wait for elements to appear and then call th

7条回答
  •  孤城傲影
    2021-01-03 11:04

    We can develop implicit wait on our own.

    Use this code; it should also work the same as implicit wait.

    //=== Start of Implicit Wait Statement ===
    
    public void implicit_Wait_ID(String str) throws Exception{
    
            for(int i=0;i<60;i++){
                try{
                    driver.findElement(By.id(str)).isDisplayed();
                    break;
                }catch(Exception e){Thread.sleep(2000);
    
                }   
            }
    }
    //=== End of Implicit Wait Statement ===    
    

    Use this method by passing the ID value:

    public void loginGmail() throws Exception
    {
            driver.findElement(By.id("Email")).sendKeys("Mail ID");
            driver.findElement(By.id("next")).click();
            implicit_Wait_ID("Passwd");
            driver.findElement(By.id("Passwd")).sendKeys("Pwd value");
            driver.findElement(By.id("signIn")).click();
    }
    

    If it is Xpath, LinkText, just create one of the above methods for all locator types and reuse it n number of times in your script.

提交回复
热议问题