一、封装一个验证元素在页面上是否存在的方法

  1 package usefulmethods;
  2 
  3 import java.util.List;
  4 
  5 import org.openqa.selenium.By;
  6 import org.openqa.selenium.WebDriver;
  7 import org.openqa.selenium.WebElement;
  8 
  9 public class GenericMethods {
 10     
 11     WebDriver driver;
 12 //    创建一个构成方法,用于初始化对象
 13     public GenericMethods(WebDriver driver) {
 14         this.driver = driver;
 15     }
 16     
 17 //    查找单个元素
 18     public WebElement getElement(String type,String locator) {
 19         if(type.equals("id")) {
 20             System.out.println("用id查找元素:"+locator);
 21             return this.driver.findElement(By.id(locator));
 22         }else if(type.equals("xpath")) {
 23             System.out.println("用xpath查找元素:"+locator);
 24             return this.driver.findElement(By.xpath(locator));
 25         }else if(type.equals("name")) {
 26             System.out.println("用name查找元素:"+locator);
 27             return this.driver.findElement(By.name(locator));
 28         }else if(type.equals("cssSelector")) {
 29             System.out.println("用cssSelector查找元素:"+locator);
 30             return this.driver.findElement(By.cssSelector(locator));
 31         }else if(type.equals("cssSelector")) {
 32             System.out.println("用cssSelector查找元素:"+locator);
 33             return this.driver.findElement(By.cssSelector(locator));
 34         }else if(type.equals("linkText")) {
 35             System.out.println("用linkText查找元素:"+locator);
 36             return this.driver.findElement(By.linkText(locator));
 37         }else if(type.equals("partialLinkText")) {
 38             System.out.println("用partialLinkText查找元素:"+locator);
 39             return this.driver.findElement(By.partialLinkText(locator));
 40         }else if(type.equals("tagName")) {
 41             System.out.println("用tagName查找元素:"+locator);
 42             return this.driver.findElement(By.tagName(locator));
 43         }else if(type.equals("className")) {
 44             System.out.println("用className查找元素:"+locator);
 45             return this.driver.findElement(By.className(locator));
 46         }else {
 47             System.out.println("定位的路径不支持");
 48             return null;
 49         }
 50     }
 51     
 52 //    查找多个元素
 53     public List<WebElement> getElementList(String type,String locator) {
 54         if(type.equals("id")) {
 55             System.out.println("用id查找元素:"+locator);
 56             return this.driver.findElements(By.id(locator));
 57         }else if(type.equals("xpath")) {
 58             System.out.println("用xpath查找元素:"+locator);
 59             return this.driver.findElements(By.xpath(locator));
 60         }else if(type.equals("name")) {
 61             System.out.println("用name查找元素:"+locator);
 62             return this.driver.findElements(By.name(locator));
 63         }else if(type.equals("cssSelector")) {
 64             System.out.println("用cssSelector查找元素:"+locator);
 65             return this.driver.findElements(By.cssSelector(locator));
 66         }else if(type.equals("cssSelector")) {
 67             System.out.println("用cssSelector查找元素:"+locator);
 68             return this.driver.findElements(By.cssSelector(locator));
 69         }else if(type.equals("linkText")) {
 70             System.out.println("用linkText查找元素:"+locator);
 71             return this.driver.findElements(By.linkText(locator));
 72         }else if(type.equals("partialLinkText")) {
 73             System.out.println("用partialLinkText查找元素:"+locator);
 74             return this.driver.findElements(By.partialLinkText(locator));
 75         }else if(type.equals("tagName")) {
 76             System.out.println("用tagName查找元素:"+locator);
 77             return this.driver.findElements(By.tagName(locator));
 78         }else if(type.equals("className")) {
 79             System.out.println("用className查找元素:"+locator);
 80             return this.driver.findElements(By.className(locator));
 81         }else {
 82             System.out.println("定位的路径不支持");
 83             return null;
 84         }    
 85     }
 86 
 87 //    验证元素在页面上是否存在
 88     public boolean isElemenetPresent(String type,String locator) {
 89 //        直接调用改类中查找元素的方法
 90         List<WebElement> elementList = getElementList(type,locator);
 91         int size = elementList.size();
 92         if(size>0) {
 93             System.out.println(locator+"在页面上匹配到的元素个数为:"+size);
 94             return true;
 95         }else {
 96             System.out.println(locator+"在页面上匹配到的元素个数为:"+size);
 97             return false;
 98         }
 99     }
100 }

 

二、实例演示

 1 package usefulmethods;
 2 
 3 import java.util.List;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 import org.junit.jupiter.api.AfterEach;
 7 import org.junit.jupiter.api.BeforeEach;
 8 import org.junit.jupiter.api.Test;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.WebElement;
11 import org.openqa.selenium.chrome.ChromeDriver;
12 
13 class GenericMethodsDemoPresent {
14     private WebDriver driver;
15     private String url;
16 //    创建一个GenericMethods类的引用
17     private GenericMethods gm;
18 
19     @BeforeEach
20     public void setUp() throws Exception {
21         driver = new ChromeDriver();
22 //        实例化查找元素的方法
23         gm = new GenericMethods(driver);
24         url = "C:\\Users\\acer\\eclipse-workspace\\SeleniumPractise\\PracticePage.html";
25         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
26         driver.manage().window().maximize();
27     }
28 
29     @Test
30     public void test() {
31         driver.get(url);
32 //        判断元素是否存在
33 //        通过调用GenericMethods类中的isElemenetPresent方法,传入查找元素的方式和路径
34         boolean el = gm.isElemenetPresent("cssSelector", "#name");
35         System.out.println("页面元素存在情况为:"+el+"\n");
36         
37         boolean ell = gm.isElemenetPresent("cssSelector", "#ne");
38         System.out.println("页面元素存在情况为:"+ell);
39     }
40 
41     @AfterEach
42     public void tearDown() throws Exception {
43         Thread.sleep(2000);
44         driver.quit();
45     }
46 }

运行结果为:

用cssSelector查找元素:#name
#name在页面上匹配到的元素个数为:1
页面元素存在情况为:true

用cssSelector查找元素:#ne
#ne在页面上匹配到的元素个数为:0
页面元素存在情况为:false

 

如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴可以相互一起学习讨论。

posted on 2019-05-13 16:09  时光以北暮南城  阅读(364)  评论(0编辑  收藏  举报