PO设计模式实例

文件结构

 页面定位元素:zentao.properties

 1 #login page
 2 username = //*[@id='account']
 3 password = //*[@name='password']
 4 loginbt = //*[@id='submit']
 5 
 6 #navigation
 7 productwindow=//*[contains(@data-id,'product')]
 8 creat=//*[contains(@data-id,'creat')]
 9 
10 #product create page
11 productname=//*[@id='name']
12 productcode=//*[@id='code']
13 productowner=//*[@id='PO']
14 producttype=//*[@id='type']
15 productdescription=/html/body
16 savebutton=//*[@id='submit']
 1 package task;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 import org.openqa.selenium.JavascriptExecutor;
 7 import org.openqa.selenium.WebDriver;
 8 import org.openqa.selenium.WebElement;
 9 import org.openqa.selenium.support.ui.Select;
10 
11 import util.ElementLocation;
12 import util.ReadProperties;
13 
14 public class CreatNewProduct {
15     
16     public static void creat_new_product(WebDriver dr,String prodname,String prodcode) throws FileNotFoundException, IOException{
17         
18         //跳转进入产品界面
19         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productwindow")).click();
20         //点击添加产品
21         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "creat")).click();
22         
23         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productname")).sendKeys(prodname);
24         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productcode")).sendKeys(prodcode);
25         
26         ((JavascriptExecutor)dr).executeScript("document.getElementById('PO').style.display='block'");
27         WebElement po = ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productowner"));
28         Select productowner = new Select(po);
29         productowner.selectByValue("admin");
30 
31         //产品描述
32         dr.switchTo().frame(0);
33         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productdescription")).sendKeys("这是一个Java UI实战");
34         dr.switchTo().defaultContent();
35 
36         //点击保存
37         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "savebutton")).click();
38                 

流程操作

1、新建产品

 1 package task;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 import org.apache.xmlbeans.SchemaLocalElement;
 7 import org.openqa.selenium.JavascriptExecutor;
 8 import org.openqa.selenium.WebDriver;
 9 import org.openqa.selenium.WebElement;
10 import org.openqa.selenium.support.ui.Select;
11 
12 import util.ElementLocation;
13 import util.ReadProperties;
14 
15 public class CreatNewProduct {
16     
17     public static void creat_new_product(WebDriver dr,String prodname,String prodcode) throws FileNotFoundException, IOException{
18         
19         //跳转进入产品界面
20         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productwindow")).click();
21         //点击添加产品
22         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "creat")).click();
23         
24         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productname")).sendKeys(prodname);
25         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productcode")).sendKeys(prodcode);
26         
27         ((JavascriptExecutor)dr).executeScript("document.getElementById('PO').style.display='block'");
28         WebElement po = ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productowner"));
29         Select productowner = new Select(po);
30         productowner.selectByValue("admin");
31         
32         
33         //产品描述
34         
35         dr.switchTo().frame(0);
36         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productdescription")).sendKeys("这是一个Java UI实战");
37         dr.switchTo().defaultContent();
38         
39         
40         //点击保存
41         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "savebutton")).click();
42                 
43     }
44 }
 1 package task;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 import org.openqa.selenium.JavascriptExecutor;
 7 import org.openqa.selenium.WebDriver;
 8 import org.openqa.selenium.WebElement;
 9 import org.openqa.selenium.support.ui.Select;
10 
11 import util.ElementLocation;
12 import util.ReadProperties;
13 
14 public class CreatNewProduct {
15     
16     public static void creat_new_product(WebDriver dr,String prodname,String prodcode) throws FileNotFoundException, IOException{
17         
18         //跳转进入产品界面
19         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productwindow")).click();
20         //点击添加产品
21         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "creat")).click();
22         
23         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productname")).sendKeys(prodname);
24         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productcode")).sendKeys(prodcode);
25         
26         ((JavascriptExecutor)dr).executeScript("document.getElementById('PO').style.display='block'");
27         WebElement po = ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productowner"));
28         Select productowner = new Select(po);
29         productowner.selectByValue("admin");
30 
31         //产品描述
32         dr.switchTo().frame(0);
33         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "productdescription")).sendKeys("这是一个Java UI实战");
34         dr.switchTo().defaultContent();
35 
36         //点击保存
37         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "savebutton")).click();
38                 
39     }
40 

2、登录操作: LoginToZentao

 1 package task;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 import org.openqa.selenium.WebDriver;
 7 
 8 import util.ElementLocation;
 9 import util.ReadProperties;
10 
11 public class LoginToZentao {
12     
13     public static void login_to_zentao(WebDriver dr,String username,String password) throws FileNotFoundException, IOException{
14         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "username")).clear();
15         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "username")).sendKeys(username);
16         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "password")).clear();
17         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "password")).sendKeys(password);
18         ElementLocation.locate(dr, "xpath", ReadProperties.getprop("zentao", "loginbt")).click();
19     }
20 }

常用方法

1、登录:BrowserSelector

 1 package util;
 2 
 3 import org.openqa.selenium.WebDriver;
 4 import org.openqa.selenium.firefox.FirefoxDriver;
 5 
 6 public class BrowserSelector {
 7     public static WebDriver browser(WebDriver dr,String browsername,String url) throws InterruptedException {
 8         if(browsername.equals("Firfox")){
 9             System.setProperty("webdriver.gecko.driver", "D://geckodriver.exe");
10             System.setProperty("webdriver.firefox.bin","D://firefox.exe");
11             dr = new FirefoxDriver();
12         }
13         dr.manage().window().maximize();
14         dr.get(url);
15         Thread.sleep(1000);
16         return dr;
17     }
18 }

2、元素定位:ElementLocation

 1 package util;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.support.ui.ExpectedConditions;
 7 import org.openqa.selenium.support.ui.WebDriverWait;
 8 
 9 public class ElementLocation {
10     
11     public static WebElement locate(WebDriver dr,String locatetype,String locatepath) {
12         
13         WebElement el = null;
14         WebDriverWait wait = new WebDriverWait(dr,5);
15         
16         if(locatetype.equals("id")){
17             wait.until(ExpectedConditions.elementToBeClickable(By.id(locatepath)));
18             el = dr.findElement(By.id(locatepath));
19         }
20         else if(locatetype.equals("xpath")){
21             wait.until(ExpectedConditions.elementToBeClickable(By.xpath(locatepath)));
22             el = dr.findElement(By.xpath(locatepath));
23         }
24         else if(locatetype.equals("name")){
25             wait.until(ExpectedConditions.elementToBeClickable(By.name(locatepath)));
26             el = dr.findElement(By.name(locatepath));
27         }
28         else if(locatetype.equals("classname")){
29             wait.until(ExpectedConditions.elementToBeClickable(By.className(locatepath)));
30             el = dr.findElement(By.className(locatepath));
31         }
32         return el;
33         
34     }
35 }

3、读取Excel:RangeDatabyPOI

 1 package util;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
10 import org.apache.poi.ss.usermodel.Cell;
11 import org.apache.poi.ss.usermodel.Row;
12 import org.apache.poi.ss.usermodel.Sheet;
13 import org.apache.poi.ss.usermodel.Workbook;
14 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
15 
16 public class RangeDatabyPOI {
17     
18     public static Object[][] poiRangeData(String filePath) throws IOException {
19         
20         File file = new File( filePath);
21         //读取数据流
22         FileInputStream inputStream = new FileInputStream(file);
23         Workbook workbook = null;
24         String extensionName = filePath.substring(filePath.indexOf("."));
25         if(extensionName.equals(".xls")){
26             workbook = new HSSFWorkbook(inputStream);
27         }
28         else if(extensionName.equals(".xlsx")){
29             workbook = new XSSFWorkbook(inputStream);
30         }
31         else{
32             System.out.println("文件格式不正确");
33         }
34         Sheet sheet = workbook.getSheetAt(0);
35         int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
36         List<Object[]> records = new ArrayList<Object[]>();
37         
38         for(int i=1;i<rowCount+1;i++){
39             Row row = sheet.getRow(i);
40             String fields[] = new String[row.getLastCellNum()];
41             for(int j=0;j<row.getLastCellNum();j++){
42                 row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
43                 fields[j] = row.getCell(j).getStringCellValue();
44             }
45             records.add(fields);
46         }
47         Object[][] results = new Object[records.size()][];
48         for(int i=0;i<records.size();i++){
49             results[i] = records.get(i);
50         }
51         return results;
52         
53     }
54     public static void main(String[] args) throws IOException {
55         Object[][] array = poiRangeData("E:\\Java\\eclipse\\MavenTest\\src\\test\\java\\util\\test1.xlsx");
56         System.out.println(array);
57     }
58 }

4、读取配置文件:ReadProperties

 1 package util;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.util.Properties;
 7 
 8 public class ReadProperties {
 9     
10     public static String getprop(String filename,String propname) throws FileNotFoundException, IOException{
11         Properties props = new Properties();
12         props.load(new FileInputStream("../target/test-classes/element/"+filename+".properties"));
13         String str = props.getProperty(propname);
14         return str;
15     }
16 }

测试用例:testcase

 1 package testcase;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 import org.openqa.selenium.WebDriver;
 6 import org.testng.annotations.BeforeTest;
 7 import org.testng.annotations.DataProvider;
 8 import org.testng.annotations.Parameters;
 9 import org.testng.annotations.Test;
10 import org.testng.log4testng.Logger;
11 
12 import task.CreatNewProduct;
13 import task.LoginToZentao;
14 import util.BrowserSelector;
15 import util.RangeDatabyPOI;
16 
17 public class zt_CreateProduct {
18     
19     private static Logger log = Logger.getLogger(Test.class);
20     
21     WebDriver dr = null;
22     String starturl;
23     String browser;
24     String name;
25     String pass;
26     @Parameters({"url","browser","usrname","usrpass"})
27     @BeforeTest
28     public void beforetestng(String url,String br,String usrname,String usrpass) throws InterruptedException {
29         
30         starturl = url;
31         browser = br;
32         name = usrname;
33         pass = usrpass;
34         
35     }
36 /*    
37     @DataProvider(name="exceltest")
38     public Object[][] excelread() throws IOException{
39         Object[][] array = RangeDatabyPOI.poiRangeData("E:\\Java\\eclipse\\MavenTest\\src\\test\\java\\util\\test1.xlsx");
40         return array;
41     }
42     @Test(dataProvider="exceltest")
43     public void ctestng(String content1,String conten2) throws InterruptedException, FileNotFoundException, IOException{
44         dr = BrowserSelector.browser(dr, browser, starturl);
45         LoginToZentao.login_to_zentao(dr, name, pass);
46     }
47     */
48     @Test
49     public void ctestng() throws InterruptedException, FileNotFoundException, IOException{
50         dr = BrowserSelector.browser(dr, browser, starturl);
51         LoginToZentao.login_to_zentao(dr, name, pass);
52         CreatNewProduct.creat_new_product(dr, "UItest002", "UItest00002");
53     }
54 }

 

posted @ 2020-10-09 23:08  放牛的猩猩灬  阅读(394)  评论(0编辑  收藏  举报