• WebDriver API 实例详解(二)


    十一、双击某个元素

    被测试网页的html源码:

    1 <html>
    2 <head>
    3 <meta charset="UTF-8">
    4 </head>
    5 <body>
    6     <input type="text" id="inputBox" 
    7     ondblclick="javascript:this.style.background='red'">请双击</input>
    8 </body>
    9 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 
     9 import org.openqa.selenium.By;
    10 import org.openqa.selenium.WebDriver;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 import org.openqa.selenium.interactions.Actions;
    14 import org.testng.annotations.AfterMethod;
    15 
    16 public class ChormeOpen {
    17     WebDriver driver;
    18   @Test
    19   public void opentest() {
    20       File file = new File("");
    21       System.out.println(file.getAbsolutePath());
    22       String url = file.getAbsolutePath() + "/html/" + "file3.html";
    23       driver.get(url);
    24       System.out.println(driver.getCurrentUrl());
    25       //
    26       WebElement inputBox = driver.findElement(By.id("inputBox"));
    27       //声明Action对象
    28       Actions builder = new Actions(driver);
    29       //双击
    30       builder.doubleClick(inputBox).build().perform();
    31       try {
    32         Thread.sleep(3000);
    33     } catch (InterruptedException e) {
    34         // TODO Auto-generated catch block
    35         e.printStackTrace();
    36     }
    37   }
    38   @BeforeMethod
    39   public void beforeMethod() {
    40       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    41         driver = new ChromeDriver();
    42   }
    43 
    44   @AfterMethod
    45   public void afterMethod() {
    46       driver.quit();
    47   }
    48 
    49 }
    View Code

    十二、操作单选下拉列表

    被测试网页的html源码:

     1 <html>
     2 <head>
     3 <meta charset="UTF-8">
     4 </head>
     5 <body>
     6     <select name="fruit" size="1">
     7         <option id="peach" value="taozi">桃子</option>
     8         <option id="watermelon" value="xigua">西瓜</option>
     9         <option id="orange" value="juzi">橘子</option>
    10         <option id="kiwifruit" value="mihoutao">猕猴桃</option>
    11         <option id="maybush" value="shanzha">山楂</option>
    12         <option id="litchi" value="lizhi">荔枝</option>
    13     </select>
    14 </body>
    15 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 
     9 import org.openqa.selenium.By;
    10 import org.openqa.selenium.WebDriver;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 import org.openqa.selenium.interactions.Actions;
    14 import org.openqa.selenium.support.ui.Select;
    15 import org.testng.Assert;
    16 import org.testng.annotations.AfterMethod;
    17 
    18 public class ChormeOpen {
    19     WebDriver driver;
    20   @Test
    21   public void opentest() {
    22       File file = new File("");
    23       System.out.println(file.getAbsolutePath());
    24       String url = file.getAbsolutePath() + "/html/" + "file4.html";
    25       driver.get(url);
    26       System.out.println(driver.getCurrentUrl());
    27       //
    28       WebElement fruit = driver.findElement(By.name("fruit"));
    29       Select droplist = new Select(fruit);
    30       //根据Index,下标从0开始
    31       droplist.selectByIndex(3);
    32       Assert.assertEquals("猕猴桃", droplist.getFirstSelectedOption().getText());
    33       //根据value属性值
    34       droplist.selectByValue("shanzha");
    35       Assert.assertEquals("山楂", droplist.getFirstSelectedOption().getText());
    36       //通过显示的文字
    37       droplist.selectByVisibleText("荔枝");
    38       Assert.assertEquals("荔枝", droplist.getFirstSelectedOption().getText());
    39       try {
    40         Thread.sleep(3000);
    41     } catch (InterruptedException e) {
    42         // TODO Auto-generated catch block
    43         e.printStackTrace();
    44     }
    45   }
    46   @BeforeMethod
    47   public void beforeMethod() {
    48       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    49         driver = new ChromeDriver();
    50   }
    51 
    52   @AfterMethod
    53   public void afterMethod() {
    54       driver.quit();
    55   }
    56 
    57 }
    View Code

    十三、检查单选列表的选项文字是否符合期望

    被测试网页的html源码:

     1 <html>
     2 <head>
     3 <meta charset="UTF-8">
     4 </head>
     5 <body>
     6     <select name="fruit" size="1">
     7         <option id="peach" value="taozi">桃子</option>
     8         <option id="watermelon" value="xigua">西瓜</option>
     9         <option id="orange" value="juzi">橘子</option>
    10         <option id="kiwifruit" value="mihoutao">猕猴桃</option>
    11         <option id="maybush" value="shanzha">山楂</option>
    12         <option id="litchi" value="lizhi">荔枝</option>
    13     </select>
    14 </body>
    15 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 import java.util.ArrayList;
     9 import java.util.Arrays;
    10 import java.util.List;
    11 
    12 import org.openqa.selenium.By;
    13 import org.openqa.selenium.WebDriver;
    14 import org.openqa.selenium.WebElement;
    15 import org.openqa.selenium.chrome.ChromeDriver;
    16 import org.openqa.selenium.interactions.Actions;
    17 import org.openqa.selenium.support.ui.Select;
    18 import org.testng.Assert;
    19 import org.testng.annotations.AfterMethod;
    20 
    21 public class ChormeOpen {
    22     WebDriver driver;
    23   @Test
    24   public void opentest() {
    25       File file = new File("");
    26       System.out.println(file.getAbsolutePath());
    27       String url = file.getAbsolutePath() + "/html/" + "file4.html";
    28       driver.get(url);
    29       System.out.println(driver.getCurrentUrl());
    30       //
    31       WebElement fruit = driver.findElement(By.name("fruit"));
    32       Select droplist = new Select(fruit);
    33       //
    34       List<String> exp_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"}));
    35       List<String> act_option = new ArrayList<String>();
    36       for(WebElement option:droplist.getOptions()){
    37           act_option.add(option.getText());
    38       }
    39       //断言
    40       Assert.assertEquals(exp_options.toArray(), act_option.toArray());
    41       try {
    42         Thread.sleep(3000);
    43     } catch (InterruptedException e) {
    44         // TODO Auto-generated catch block
    45         e.printStackTrace();
    46     }
    47   }
    48   @BeforeMethod
    49   public void beforeMethod() {
    50       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    51         driver = new ChromeDriver();
    52   }
    53 
    54   @AfterMethod
    55   public void afterMethod() {
    56       driver.quit();
    57   }
    58 
    59 }
    View Code

    十四、操作多选的选择列表

    被测试网页的html源码:

     1 <html>
     2 <head>
     3 <meta charset="UTF-8">
     4 </head>
     5 <body>
     6     <select name="fruit" size="6" multiple=true>
     7         <option id="peach" value="taozi">桃子</option>
     8         <option id="watermelon" value="xigua">西瓜</option>
     9         <option id="orange" value="juzi">橘子</option>
    10         <option id="kiwifruit" value="mihoutao">猕猴桃</option>
    11         <option id="maybush" value="shanzha">山楂</option>
    12         <option id="litchi" value="lizhi">荔枝</option>
    13     </select>
    14 </body>
    15 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 
     9 import org.openqa.selenium.By;
    10 import org.openqa.selenium.WebDriver;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 import org.openqa.selenium.support.ui.Select;
    14 import org.testng.annotations.AfterMethod;
    15 
    16 public class ChormeOpen {
    17     WebDriver driver;
    18   @Test
    19   public void opentest() {
    20       File file = new File("");
    21       System.out.println(file.getAbsolutePath());
    22       String url = file.getAbsolutePath() + "/html/" + "file5.html";
    23       driver.get(url);
    24       System.out.println(driver.getCurrentUrl());
    25       //
    26       WebElement fruit = driver.findElement(By.name("fruit"));
    27       Select droplist = new Select(fruit);
    28       //选择
    29       droplist.selectByIndex(3);
    30       droplist.selectByValue("shanzha");
    31       droplist.selectByVisibleText("桃子");
    32       droplist.deselectAll();//取消全部选择
    33       
    34       //再次选择
    35       droplist.selectByIndex(3);
    36       droplist.selectByValue("shanzha");
    37       droplist.selectByVisibleText("桃子");
    38       
    39       //逐个取消
    40       droplist.deselectByIndex(3);
    41       droplist.deselectByValue("shanzha");
    42       droplist.deselectByVisibleText("桃子");
    43       
    44       try {
    45         Thread.sleep(3000);
    46     } catch (InterruptedException e) {
    47         // TODO Auto-generated catch block
    48         e.printStackTrace();
    49     }
    50   }
    51   @BeforeMethod
    52   public void beforeMethod() {
    53       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    54         driver = new ChromeDriver();
    55   }
    56 
    57   @AfterMethod
    58   public void afterMethod() {
    59       driver.quit();
    60   }
    61 
    62 }
    View Code

     十五、操作单选框

    被测试网页的html源码:

     1 <html>
     2 <head>
     3 <meta charset="UTF-8">
     4 </head>
     5 <body>
     6     <form>
     7         <input type="radio" name="fruit" value="berry">草莓</input>
     8         <br/>
     9         <input type="radio" name="fruit" value="watermelon">西瓜</input>
    10         <br/>
    11         <input type="radio" name="fruit" value="orange">橙子</input>
    12     </form>
    13 </body>
    14 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 import java.util.List;
     9 
    10 import org.openqa.selenium.By;
    11 import org.openqa.selenium.WebDriver;
    12 import org.openqa.selenium.WebElement;
    13 import org.openqa.selenium.chrome.ChromeDriver;
    14 import org.testng.Assert;
    15 import org.testng.annotations.AfterMethod;
    16 
    17 public class ChormeOpen {
    18     WebDriver driver;
    19   @Test
    20   public void opentest() {
    21       File file = new File("");
    22       System.out.println(file.getAbsolutePath());
    23       String url = file.getAbsolutePath() + "/html/" + "file6.html";
    24       driver.get(url);
    25       System.out.println(driver.getCurrentUrl());
    26       //
    27       WebElement radioOption = driver.findElement(By.xpath("//input[@value='berry']"));
    28       if(!radioOption.isSelected()){
    29           radioOption.click();
    30       }
    31       Assert.assertTrue(radioOption.isSelected());
    32       //
    33       List<WebElement> fruits = driver.findElements(By.name("fruit"));
    34       for(WebElement fruit:fruits){
    35           if(fruit.getAttribute("value").equals("watermelon")){
    36               if(!fruit.isSelected()){
    37                   fruit.click();
    38               }
    39               Assert.assertTrue(fruit.isSelected());
    40               break;
    41           }
    42       }
    43       try {
    44         Thread.sleep(3000);
    45     } catch (InterruptedException e) {
    46         // TODO Auto-generated catch block
    47         e.printStackTrace();
    48     }
    49   }
    50   @BeforeMethod
    51   public void beforeMethod() {
    52       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    53         driver = new ChromeDriver();
    54   }
    55 
    56   @AfterMethod
    57   public void afterMethod() {
    58       driver.quit();
    59   }
    60 
    61 }
    View Code

    十六、操作复选框

    被测试网页的html源码:

     1 <html>
     2 <head>
     3 <meta charset="UTF-8">
     4 </head>
     5 <body>
     6     <form>
     7         <input type="checkbox" name="fruit" value="berry">草莓</input>
     8         <br/>
     9         <input type="checkbox" name="fruit" value="watermelon">西瓜</input>
    10         <br/>
    11         <input type="checkbox" name="fruit" value="orange">橙子</input>
    12     </form>
    13 </body>
    14 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 import java.util.List;
     9 
    10 import org.openqa.selenium.By;
    11 import org.openqa.selenium.WebDriver;
    12 import org.openqa.selenium.WebElement;
    13 import org.openqa.selenium.chrome.ChromeDriver;
    14 import org.testng.Assert;
    15 import org.testng.annotations.AfterMethod;
    16 
    17 public class ChormeOpen {
    18     WebDriver driver;
    19   @Test
    20   public void opentest() {
    21       File file = new File("");
    22       System.out.println(file.getAbsolutePath());
    23       String url = file.getAbsolutePath() + "/html/" + "file7.html";
    24       driver.get(url);
    25       System.out.println(driver.getCurrentUrl());
    26       //
    27       WebElement orangcheckbox = driver.findElement(By.xpath("//input[@value='orange']"));
    28       if(!orangcheckbox.isSelected()){
    29           orangcheckbox.click();
    30       }
    31       Assert.assertTrue(orangcheckbox.isSelected());
    32       //
    33       List<WebElement> checkboxs = driver.findElements(By.name("fruit"));
    34       for(WebElement checkbox:checkboxs){
    35           checkbox.click();
    36       }
    37       try {
    38         Thread.sleep(3000);
    39     } catch (InterruptedException e) {
    40         // TODO Auto-generated catch block
    41         e.printStackTrace();
    42     }
    43   }
    44   @BeforeMethod
    45   public void beforeMethod() {
    46       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    47         driver = new ChromeDriver();
    48   }
    49 
    50   @AfterMethod
    51   public void afterMethod() {
    52       driver.quit();
    53   }
    54 
    55 }
    View Code

    十七、检查页面元素的文本内容是否出现

    被测试网页的HTML代码:

    1 <html>
    2 <head>
    3 <meta charset="UTF-8">
    4 </head>
    5 <body>
    6     <p>《三生三世十里桃花》这个电影真的很棒!</p>
    7     <p>主要是杨洋不错!</p>
    8 </body>
    9 </html>
    View Code

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 
     7 import java.io.File;
     8 import org.openqa.selenium.By;
     9 import org.openqa.selenium.WebDriver;
    10 import org.openqa.selenium.WebElement;
    11 import org.openqa.selenium.chrome.ChromeDriver;
    12 import org.testng.Assert;
    13 import org.testng.annotations.AfterMethod;
    14 
    15 public class ChormeOpen {
    16     WebDriver driver;
    17   @Test
    18   public void opentest() {
    19       File file = new File("");
    20       System.out.println(file.getAbsolutePath());
    21       String url = file.getAbsolutePath() + "/html/" + "file8.html";
    22       driver.get(url);
    23       System.out.println(driver.getCurrentUrl());
    24       //
    25       WebElement text = driver.findElement(By.xpath("//p[1]"));
    26       String contentText = text.getText();
    27       Assert.assertEquals("《三生三世十里桃花》这个电影真的很棒!", contentText);
    28       Assert.assertTrue(contentText.contains("三生三世"));
    29       Assert.assertTrue(contentText.startsWith("《三"));
    30       Assert.assertTrue(contentText.endsWith("很棒!"));
    31       try {
    32         Thread.sleep(3000);
    33     } catch (InterruptedException e) {
    34         // TODO Auto-generated catch block
    35         e.printStackTrace();
    36     }
    37   }
    38   @BeforeMethod
    39   public void beforeMethod() {
    40       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    41         driver = new ChromeDriver();
    42   }
    43 
    44   @AfterMethod
    45   public void afterMethod() {
    46       driver.quit();
    47   }
    48 
    49 }
    View Code

    十八、执行javaScript脚本

    被测试网页的网址:

    http://baidu.com

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 import org.openqa.selenium.JavascriptExecutor;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.chrome.ChromeDriver;
     9 import org.testng.Assert;
    10 import org.testng.annotations.AfterMethod;
    11 
    12 public class ChormeOpen {
    13     WebDriver driver;
    14     String url = "http://www.baidu.com";
    15   @Test
    16   public void opentest() {
    17       driver.get(url);
    18       JavascriptExecutor js = (JavascriptExecutor) driver;
    19       String title = (String) js.executeScript("return document.title");
    20       Assert.assertEquals("百度一下,你就知道", title);
    21       
    22       try {
    23         Thread.sleep(3000);
    24     } catch (InterruptedException e) {
    25         // TODO Auto-generated catch block
    26         e.printStackTrace();
    27     }
    28       String searchText = (String) js.executeScript("var button= document.getElementById('su');return button.value");
    29       System.out.println(searchText);
    30   }
    31   @BeforeMethod
    32   public void beforeMethod() {
    33       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    34         driver = new ChromeDriver();
    35   }
    36 
    37   @AfterMethod
    38   public void afterMethod() {
    39       driver.quit();
    40   }
    41 
    42 }
    View Code

    十九、拖曳页面元素

    被测试网页的网址:

    http://jqueryui.com/resources/demos/draggable/scroll.html

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 import org.openqa.selenium.By;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.WebElement;
     9 import org.openqa.selenium.chrome.ChromeDriver;
    10 import org.openqa.selenium.interactions.Actions;
    11 import org.testng.annotations.AfterMethod;
    12 
    13 public class ChormeOpen {
    14     WebDriver driver;
    15     String url = "http://jqueryui.com/resources/demos/draggable/scroll.html";
    16 
    17     @Test
    18     public void opentest() {
    19         driver.get(url);
    20         WebElement draggable = driver.findElement(By.id("draggable"));
    21         //向下拖动10个像素、共拖动5次
    22         for(int i=0;i<5;i++){
    23             new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
    24         }
    25         //向右拖动10个像素、共拖动5次
    26         for(int i=0;i<5;i++){
    27             new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();;
    28         }
    29         try {
    30             Thread.sleep(3000);
    31         } catch (InterruptedException e) {
    32             // TODO Auto-generated catch block
    33             e.printStackTrace();
    34         }
    35     }
    36     
    37   @BeforeMethod
    38   public void beforeMethod() {
    39       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    40         driver = new ChromeDriver();
    41   }
    42 
    43   @AfterMethod
    44   public void afterMethod() {
    45       driver.quit();
    46   }
    47 
    48 }
    View Code

    二十、模拟键盘的操作

    被测试网页的网址:

    http://www.baidu.com

    Java语言版本的API实例代码:

     1 package test;
     2 
     3 import org.testng.annotations.Test;
     4 
     5 import org.testng.annotations.BeforeMethod;
     6 import org.openqa.selenium.Keys;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.chrome.ChromeDriver;
     9 import org.openqa.selenium.interactions.Actions;
    10 import org.testng.annotations.AfterMethod;
    11 
    12 public class ChormeOpen {
    13     WebDriver driver;
    14     String url = "http://www.baidu.com";
    15 
    16     @Test
    17     public void opentest() {
    18         driver.get(url);
    19         Actions action = new Actions(driver);
    20         action.keyDown(Keys.CONTROL);//按下Ctrl键
    21         action.keyDown(Keys.SHIFT);//按下Shift键
    22         action.keyDown(Keys.ALT);//按下Alt键
    23         action.keyUp(Keys.CONTROL);//释放Ctrl键
    24         action.keyUp(Keys.SHIFT);//释放Shift键
    25         action.keyUp(Keys.ALT);//释放ALT键
    26         //模拟键盘在搜索输入框输入大写的字符“ABCD”
    27         action.keyDown(Keys.SHIFT).sendKeys("abcd").perform();
    28         try {
    29             Thread.sleep(3000);
    30         } catch (InterruptedException e) {
    31             // TODO Auto-generated catch block
    32             e.printStackTrace();
    33         }
    34     }
    35     
    36   @BeforeMethod
    37   public void beforeMethod() {
    38       System.setProperty("webdriver.chrome.driver", "F:\selenium\chromedriver.exe");
    39         driver = new ChromeDriver();
    40   }
    41 
    42   @AfterMethod
    43   public void afterMethod() {
    44       driver.quit();
    45   }
    46 
    47 }
    View Code
  • 相关阅读:
    【前端开发】vue子项目打包一个组件供另一个项目使用教程
    【前端开发】基于flow-editor-vue库改造的流程设计器,审批流程引擎前端教程
    【前端开发】基于logicFlow可视化流程库改造的流程引擎教程
    知识蒸馏:Distillation
    浮点神经网络vs二值神经网络
    Representation Learning 表征学习
    mybatis plus 追加where 函数
    20211012 MapStruct
    20211012 Dubbo 的 SPI 和 Adaptive
    20210916 小马哥讲 Spring AOP
  • 原文地址:https://www.cnblogs.com/successcai/p/6661708.html
Copyright © 2020-2023  润新知