zoukankan      html  css  js  c++  java
  • 章节十三、2-隐式等待、显式等待--实例演示

    一、隐式等待

    https://www.yahoo.com/的登录为例,需要在用户名输入框中输入“test”

    我们先用未设置隐式等待的代码看看执行效果:

     1 package waittyps;
     2 
     3 import org.junit.jupiter.api.AfterEach;
     4 import org.junit.jupiter.api.BeforeEach;
     5 import org.junit.jupiter.api.Test;
     6 import org.openqa.selenium.By;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.ie.InternetExplorerDriver;
     9 
    10 class ImplicitWaitDemo {
    11     private WebDriver driver;
    12     private String url;
    13 
    14     @BeforeEach
    15     void setUp() throws Exception {
    16         driver = new InternetExplorerDriver();
    17         url = "https://www.yahoo.com/";
    18         driver.manage().window().maximize();
    19     }
    20 
    21     @Test
    22     void test() {
    23         driver.get(url);
    24         driver.findElement(By.id("uh-signin")).click();
    25         driver.findElement(By.cssSelector("#login-username")).sendKeys("test");
    26     }
    27     
    28     @AfterEach
    29     void tearDown() throws Exception {
    30         Thread.sleep(2000);
    31         driver.quit();
    32     }
    33 }

    执行结果:执行失败,提示未定位到输入框元素(元素路径是没有问题的,因为未设置等待时间,所以没有找到“login-username”,导致控制台直接报错)

     现在我们设置隐式等待时间3秒,然后运行代码:

     1 package waittyps;
     2 
     3 import java.util.concurrent.TimeUnit;
     4 
     5 import org.junit.jupiter.api.AfterEach;
     6 import org.junit.jupiter.api.BeforeEach;
     7 import org.junit.jupiter.api.Test;
     8 import org.openqa.selenium.By;
     9 import org.openqa.selenium.WebDriver;
    10 import org.openqa.selenium.ie.InternetExplorerDriver;
    11 
    12 class ImplicitWaitDemo {
    13     private WebDriver driver;
    14     private String url;
    15 
    16     @BeforeEach
    17     void setUp() throws Exception {
    18         driver = new InternetExplorerDriver();
    19         url = "https://www.yahoo.com/";
    20         driver.manage().window().maximize();
    21 //        隐式等待3秒
    22         driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    23     }
    24 
    25     @Test
    26     void test() {
    27         driver.get(url);
    28         driver.findElement(By.id("uh-signin")).click();
    29         driver.findElement(By.cssSelector("#login-username")).sendKeys("test");
    30     }
    31     
    32     @AfterEach
    33     void tearDown() throws Exception {
    34         Thread.sleep(2000);
    35         driver.quit();
    36     }
    37 }

    运行结果:成功

    二、显式等待

     1 package waittyps;
     2 
     3 import org.junit.jupiter.api.AfterEach;
     4 import org.junit.jupiter.api.BeforeEach;
     5 import org.junit.jupiter.api.Test;
     6 import org.openqa.selenium.By;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.WebElement;
     9 import org.openqa.selenium.ie.InternetExplorerDriver;
    10 import org.openqa.selenium.support.ui.ExpectedConditions;
    11 import org.openqa.selenium.support.ui.WebDriverWait;
    12 
    13 class ImplicitWaitDemo {
    14     private WebDriver driver;
    15     private String url;
    16 
    17     @BeforeEach
    18     void setUp() throws Exception {
    19         driver = new InternetExplorerDriver();
    20         url = "https://www.yahoo.com/";
    21         driver.manage().window().maximize();
    22 //        隐式等待3秒
    23 //        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    24     }
    25 
    26     @Test
    27     void test() {
    28         driver.get(url);
    29         WebElement el = driver.findElement(By.id("uh-signin"));
    30         el.click();
    31 //        实例化WebDriverWait对象,括号中内容表示(作用于谁,显式等待时长)
    32         WebDriverWait wait = new WebDriverWait(driver,2);
    33 //        用emailField接受查找到的元素,visibilityOfElementLocated表示元素可见后再执行输入操作
    34 //        使用显式等待3秒,直到#login-username元素可用,如果在3秒内该元素可用就执行后面的操作,否则超过3秒直接抛出异常
    35         WebElement emailField = wait.until(
    36                 ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#login-username")));
    37         emailField.sendKeys("test");
    38 //        driver.findElement(By.cssSelector("#login-username")).sendKeys("test");
    39     }
    40     
    41     @AfterEach
    42     void tearDown() throws Exception {
    43         Thread.sleep(2000);
    44         driver.quit();
    45     }
    46 }
  • 相关阅读:
    java面向对象之封装
    摘抄java基础
    gojs 去除水印个人总结的方法 实例为2.1版本
    引入CSS的方式,link与@import的区别
    Java 处理json字符串value中多余的双引号
    《Linux 学习》01---redis安装, 并使用Redis Desktop Manager 连接
    (二、下) springBoot 、maven 、mysql、 mybatis、 通用Mapper、lombok 简单搭建例子 《附项目源码》
    (一 、上)搭建简单的SpringBoot + java + maven + mysql + Mybatis+通用Mapper 《附项目源码》
    springBoot 官方整合的redis 使用教程:(StringRedisTemplate 方式存储 Object类型value)
    sudo命令: 在其他用户下操作root用户权限
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/10860445.html
Copyright © 2011-2022 走看看