第八章 Selenium Webdriver 常用的方法和属性
1.如何取到元素上的文本
package usefulMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.List;
public class GetTextDemo {
WebDriver driver = new ChromeDriver();
String url = "file:///E:/Chrome/8PracticePage.html";
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(8);
}
@Test
public void name() throws Exception{
WebElement element = driver.findElement(By.cssSelector("#product > tbody > tr:nth-child(2) > td:nth-child(2)"));
System.out.println(element.getText());
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
2.如何取到元素属性的值
package usefulMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetArrribute {
WebDriver driver = new ChromeDriver();
String url = "file:///E:/Chrome/8PracticePage.html";
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(8);
}
@Test
public void name() throws Exception{
WebElement element = driver.findElement(By.id("name"));
String type = element.getAttribute("type");
System.out.println(type);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
3.封装一个查找单独元素的方法
package usefulMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GenericMethodsDemo {
WebDriver driver = new ChromeDriver();
String url = "file:///E:/Chrome/8PracticePage.html";
private GenericMethods gm;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
driver.get(url);
gm =new GenericMethods(driver);
Thread.sleep(8);
}
@Test
public void name() throws Exception{
WebElement element = gm.getElement("name","id");
String type = element.getAttribute("type");
System.out.println(type);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
4.封装多个查找单独元素的方法
package usefulMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import javax.xml.bind.Element;
import java.util.List;
public class GenericMethodsDemo {
WebDriver driver = new ChromeDriver();
String url = "file:///E:/Chrome/8PracticePage.html";
private GenericMethods gm;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
driver.get(url);
gm =new GenericMethods(driver);
Thread.sleep(8);
}
@Test
public void testDemo() throws Exception{
List<WebElement> element = gm.getElements("//input[@type='text']","xpath");
int a= element.size();
System.out.println(a);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
4.封装检查元素是否在页面存在的方法
package usefulMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
public class GenericMethods {
WebDriver driver;
public GenericMethods(WebDriver driver) {
this.driver = driver;
}
public WebElement getElement(String locator, String type) {
type = type.toLowerCase();
if (type.equals("id")) {
System.out.println("用id查找元素" + locator);
return this.driver.findElement(By.id(locator));
} else if (type.equals("xpath")) {
System.out.println("用xpath查找元素" + locator);
return this.driver.findElement(By.xpath(locator));
} else if (type.equals("name")) {
System.out.println("用name查找元素: " + locator);
return this.driver.findElement(By.name(locator));
} else if (type.equals("css")) {
System.out.println("用css查找元素: " + locator);
return this.driver.findElement(By.cssSelector(locator));
} else if (type.equals("classname")) {
System.out.println("用classname查找元素: " + locator);
return this.driver.findElement(By.className(locator));
} else if (type.equals("tagname")) {
System.out.println("用tagname查找元素: " + locator);
return this.driver.findElement(By.tagName(locator));
} else if (type.equals("linktext")) {
System.out.println("用linktext查找元素: " + locator);
return this.driver.findElement(By.linkText(locator));
} else if (type.equals("partiallinktext")) {
System.out.println("用partiallinktext查找元素: " + locator);
return this.driver.findElement(By.partialLinkText(locator));
} else {
System.out.println("定位的路径不支持");
return null;
}
}
public List<WebElement> getElements(String locator, String type) {
type = type.toLowerCase();
if (type.equals("id")) {
System.out.println("用id查找元素" + locator);
return this.driver.findElements(By.id(locator));
} else if (type.equals("xpath")) {
System.out.println("用xpath查找元素" + locator);
return this.driver.findElements(By.xpath(locator));
} else if (type.equals("name")) {
System.out.println("用name查找元素: " + locator);
return this.driver.findElements(By.name(locator));
} else if (type.equals("css")) {
System.out.println("用css查找元素: " + locator);
return this.driver.findElements(By.cssSelector(locator));
} else if (type.equals("classname")) {
System.out.println("用classname查找元素: " + locator);
return this.driver.findElements(By.className(locator));
} else if (type.equals("tagname")) {
System.out.println("用tagname查找元素: " + locator);
return this.driver.findElements(By.tagName(locator));
} else if (type.equals("linktext")) {
System.out.println("用linktext查找元素: " + locator);
return this.driver.findElements(By.linkText(locator));
} else if (type.equals("partiallinktext")) {
System.out.println("用partiallinktext查找元素: " + locator);
return this.driver.findElements(By.partialLinkText(locator));
} else {
System.out.println("定位的路径不支持");
return null;
}
}
public boolean isElementPresent(String locator, String type) {
List<WebElement> elementList = getElements(locator, type);
int size = elementList.size();
if (size > 0) {
return true;
} else {
return false;
}
}
}
package usefulMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import javax.xml.bind.Element;
import javax.xml.bind.SchemaOutputResolver;
import java.util.List;
public class GenericMethodsDemo {
WebDriver driver = new ChromeDriver();
String url = "file:///E:/Chrome/8PracticePage.html";
private GenericMethods gm;
@Before
public void setUp() throws Exception {
driver.manage().window().maximize();
driver.get(url);
gm =new GenericMethods(driver);
Thread.sleep(8);
}
@Test
public void getElement() throws Exception{
WebElement element = gm.getElement("name","id");
String type = element.getAttribute("type");
System.out.println(type);
}
@Test
public void getElementsDemo() throws Exception{
List<WebElement> element = gm.getElements("//input[@type='text']","xpath");
int a= element.size();
System.out.println(a);
}
@Test
public void isDisplayDemo() throws Exception{
boolean a = gm.isElementPresent("//input[@type='text']","xpath");
if(a){
System.out.println("元素存在");
}else {
System.out.println("元素不存在");
}
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
5.通用方法-代码重构
package usefulMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.ArrayList;
import java.util.List;
public class NewGenericMethods {
WebDriver driver;
public NewGenericMethods(WebDriver driver) {
this.driver = driver;
}
public WebElement getElement(String locator, String type) {
type = type.toLowerCase();
if (type.equals("id")) {
System.out.println("用id查找元素:" + locator);
return this.driver.findElement(By.id(locator));
} else if (type.equals("name")) {
System.out.println("用name查找元素: " + locator);
return this.driver.findElement(By.name(locator));
} else if (type.equals("xpath")) {
System.out.println("用xpath查找元素: " + locator);
return this.driver.findElement(By.xpath(locator));
} else if (type.equals("css")) {
System.out.println("用css查找元素: " + locator);
return this.driver.findElement(By.cssSelector(locator));
} else if (type.equals("classname")) {
System.out.println("用classname查找元素: " + locator);
return this.driver.findElement(By.className(locator));
} else if (type.equals("tagname")) {
System.out.println("用tagname查找元素: " + locator);
return this.driver.findElement(By.tagName(locator));
} else if (type.equals("linktext")) {
System.out.println("用linktext查找元素: " + locator);
return this.driver.findElement(By.linkText(locator));
} else if (type.equals("partiallinktext")) {
System.out.println("用partiallinktext查找元素: " + locator);
return this.driver.findElement(By.partialLinkText(locator));
} else {
System.out.println("定位的类型不支持");
return null;
}
}
public List<WebElement> getElementList(String locator, String type) {
type = type.toLowerCase();
List<WebElement> elementList = new ArrayList<WebElement>();
if (type.equals("id")) {
elementList = this.driver.findElements(By.id(locator));
} else if (type.equals("name")) {
elementList = this.driver.findElements(By.name(locator));
} else if (type.equals("xpath")) {
elementList = this.driver.findElements(By.xpath(locator));
} else if (type.equals("css")) {
elementList = this.driver.findElements(By.cssSelector(locator));
} else if (type.equals("classname")) {
elementList = this.driver.findElements(By.className(locator));
} else if (type.equals("tagname")) {
elementList = this.driver.findElements(By.tagName(locator));
} else if (type.equals("linktext")) {
elementList = this.driver.findElements(By.linkText(locator));
} else if (type.equals("partiallinktext")) {
elementList = this.driver.findElements(By.partialLinkText(locator));
} else {
System.out.println("定位的类型不支持");
}
if (elementList.isEmpty()) {
System.out.println("用 " + type + ": " + locator + " 没有找到元素");
} else {
System.out.println("元素用 " + type + ": " + locator + " 找到了");
}
return elementList;
}
public boolean isElementPresent(String locator, String type) {
List<WebElement> elementList = getElementList(locator, type);
int size = elementList.size();
if (size > 0) {
return true;
} else {
return false;
}
}
}