从这一节开始,不讲基本的页面操作了,开始为搭建框架做准备,例如如何封装查找元素的通用方法,这个方法封装好后其它类中都可以使用封装好的这个方法来查找元素,提高代码的复用性,方便后期维护。
一、首先我们需要封装一个能够定位单个或多个元素的类
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 { 44 System.out.println("定位的路径不支持"); 45 return null; 46 } 47 } 48 49 // 查找多个元素 50 public List<WebElement> getElementList(String type,String locator) { 51 if(type.equals("id")) { 52 System.out.println("用id查找元素:"+locator); 53 return this.driver.findElements(By.id(locator)); 54 }else if(type.equals("xpath")) { 55 System.out.println("用xpath查找元素:"+locator); 56 return this.driver.findElements(By.xpath(locator)); 57 }else if(type.equals("name")) { 58 System.out.println("用name查找元素:"+locator); 59 return this.driver.findElements(By.name(locator)); 60 }else if(type.equals("cssSelector")) { 61 System.out.println("用cssSelector查找元素:"+locator); 62 return this.driver.findElements(By.cssSelector(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("linkText")) { 67 System.out.println("用linkText查找元素:"+locator); 68 return this.driver.findElements(By.linkText(locator)); 69 }else if(type.equals("partialLinkText")) { 70 System.out.println("用partialLinkText查找元素:"+locator); 71 return this.driver.findElements(By.partialLinkText(locator)); 72 }else if(type.equals("tagName")) { 73 System.out.println("用tagName查找元素:"+locator); 74 return this.driver.findElements(By.tagName(locator)); 75 }else { 76 System.out.println("定位的路径不支持"); 77 return null; 78 } 79 } 80 }
二、对图中的单个输入框进行操作,输入“测试”(图中的页面是本地的网页,小伙伴们如果需要可以加入555191854下载或者找其它的网站进行练习)
package usefulmethods; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; class GenericMethodsDemo { private WebDriver driver; private String url; // 创建一个GenericMethods类的引用 private GenericMethods gm; @BeforeEach public void setUp() throws Exception { driver = new ChromeDriver(); // 实例化查找元素的方法 gm = new GenericMethods(driver); url = "C:\\\\Users\\\\acer\\\\eclipse-workspace\\\\SeleniumPractise\\\\PracticePage.html"; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } @Test public void test() { driver.get(url); // 通过调用GenericMethods类中的getElement方法,传入查找元素的方式和路径 WebElement el = gm.getElement("cssSelector", "#name"); el.sendKeys("测试"); } @AfterEach public void tearDown() throws Exception { Thread.sleep(2000); driver.quit(); } }
运行结果为:
三、定位多个输入框
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 GenericMethodsDemoList { 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 // 通过调用GenericMethods类中的getElementList方法,传入查找元素的方式和路径 33 List<WebElement> el = gm.getElementList("xpath", "//input[@type='text']"); 34 int size = el.size(); 35 System.out.println("获取到的元素长度为:"+size); 36 } 37 38 @AfterEach 39 public void tearDown() throws Exception { 40 Thread.sleep(2000); 41 driver.quit(); 42 } 43 }
运行结果:
注意:sendKeys方法无法同时对多个输入框进行操作。
如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴可以相互一起学习讨论。