mac java+selenium 实现UI自动化2_琉璃

使用excel驱动的话首先要下载JXL.jar包,导入maven项目中,可以直接拖到resouces里,然后右键add as library;然后把excel文件也同样拖到resouces里,然后代码编写获取路径;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Date;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;


public class Test {

    public String time = null;//创建测试结果报告保存到本地

    public void createReport() throws IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        time=df.format(new Date());
        File parentDir = new File("/Users/liuli/Desktop/UIreport");//报告存储的文件地址
        parentDir.mkdir();
        String hash = "Report_" + time;
        String fileName = hash + ".txt";
        File file = new File(parentDir, fileName);
        try {
            file.createNewFile();
            System.out.print("create report successful!");
        }
        catch (IOException e){
            throw new IOException("Exception: " + e);
        }
    }
    //把元素以属性来分类编写入方法,然后遍历excel去读取详细的元素

    public void buttonClick_xpath(String path,ChromeDriver driver) throws IOException {
        try{
            if (driver.findElement(By.xpath(path))!=null){
                driver.findElement(By.xpath(path)).click();
                writeToFile(path, "is clicked");

            }
            else {
                writeToFile(path,"is not found");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
  /*  public void buttonClick_id(String path,ChromeDriver driver) throws IOException {
        try{
            if (driver.findElement(By.id(path))!=null){
                driver.findElement(By.id(path)).click();
                writeToFile(path, "is clicked");
            }
            else {
                writeToFile(path,"is not found");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }*/
    public void buttonClick_className(String path,ChromeDriver driver) throws IOException {
        try{
            if (driver.findElement(By.className(path))!=null){
                driver.findElement(By.className(path)).click();
                writeToFile(path, "is clicked");
            }
            else {
                writeToFile(path,"is not found");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    public void buttonClick_linkText(String path,ChromeDriver driver) throws IOException {
        try{
            if (driver.findElement(By.linkText(path))!=null){
                driver.findElement(By.linkText(path)).click();
                writeToFile(path, "is clicked");
            }
            else {
                writeToFile(path,"is not found");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
//输入用户名和密码
    public void input_id(String path,String input,ChromeDriver driver) throws IOException {
        try{
            if (driver.findElement(By.id(path))!=null){
                driver.findElement(By.id(path)).sendKeys(input);
                writeToFile(path, "is keyed in");
            }
            else {
                writeToFile(path,"is not keyed in");
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    public void clear(String name, ChromeDriver driver) throws IOException{
        driver.findElement(By.id(name)).clear();
        writeToFile(name, "is cleared");
    }

    public void verifyText(String text, ChromeDriver driver) throws InterruptedException, IOException {
        if (driver.getPageSource().contains(text)){
            writeToFile(text, "is found");
        }
        else{
            writeToFile(text, "is not found");
        }
    }

        //点击后浏览器返回首页
    public void back(String contents, ChromeDriver driver) throws IOException {
      try{
          driver.navigate().back();
          writeToFile(contents, "browser is backed");
      }catch (IOException e){
          e.printStackTrace();
      }

        //back(sheet.getCell(1, j).getContents(), driver);
    }
    public void wait(int milli) throws IOException, InterruptedException{
        Thread.sleep(milli);
        writeToFile(milli+"", "is slept");//死等,这个后期再优化
    }

    public void writeToFile(String item, String action) throws IOException{//写入报告
        File file=new File("/Users/liuli/Desktop/UIreport/Report_" + time + ".txt");
        BufferedWriter w = new BufferedWriter(new FileWriter(file,true));
        try{
            w.newLine();
            w.write(item+" "+action);

           // w.flush();
            w.close();
        }catch (IOException e){
            e.printStackTrace();
       }finally {
            if(w!=null){
                w.close();
            }
        }
    }


}

main方法

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.chrome.ChromeOptions;


public class Function {
    public static void main(String [] args) throws InterruptedException, IOException {
        //有人很得意的告诉我这个方法比我写的绝对路径好用,只要下载代码直接就能用了,谦虚的我接受了这个建议;
        // 建一个maven项目,不选择任何模版,把chromedriver贴到resources里,然后获取这个路径,工具跟着代码走。。。
        ClassLoader classLoader=Function.class.getClassLoader();
        URL resource=classLoader.getResource("chromedriver");
        System.out.print("文件路径"+ resource.getPath()+"\n");
        System.setProperty("webdriver.chrome.chromedriver",resource.getPath());

       // driver.manage().window().maximize();//浏览器窗口最大化---后来发现这一行代码并没有产生作用,用下面的三行代码解决了最大化的问题
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--kiosk");
        WebDriver driver = new ChromeDriver(chromeOptions);

        Test test=new Test();
        try{
            test.createReport();
        }catch (IOException e){
            throw new IOException("Exception: "+e);
        }
        //下面的三行代码是获取放在resouces文件里的data数据
        ClassLoader classLoader1=Function.class.getClassLoader();
        URL resource1=classLoader1.getResource("data2.xls");
       // System.setProperty("data2",resource1.getPath());
        File inputWorkbook=new File(resource1.getPath());
        Workbook w;
        try{
            w=Workbook.getWorkbook(inputWorkbook);
            Sheet sheet=w.getSheet(0);
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(0, j);
                String value = cell.getContents();
                //The data file first column determines which method to call from test class
                if (value.equals("getUrl")){
                    driver.get(sheet.getCell(1,j).getContents());
                }
                else if(value.equals("buttonClick_xpath")){
                    test.buttonClick_xpath(sheet.getCell(1,j).getContents(), (ChromeDriver) driver);
                }
              /*  else if(value.equals("buttonClick_css")){
                    test.buttonClick_css(sheet.getCell(1, j).getContents(), driver);
                }
                else if(value.equals("buttonClick_id")){
                    test.buttonClick_id(sheet.getCell(1, j).getContents(), (ChromeDriver) driver);
                }*/
                else if(value.equals("wait")){
                    String x = sheet.getCell(1,j).getContents();
                    test.wait(Integer.parseInt(sheet.getCell(1,j).getContents()));
                }
                else if(value.equals("clear")){
                    test.clear(sheet.getCell(1,j).getContents(), (ChromeDriver) driver);
                }
               /* else if(value.equals("input_css")){
                    test.(sheet.getCell(1,j).getContents(),sheet.getCell(2,j).getContents(),driver);
                }*/
                else if(value.equals("input_id")){
                    test.input_id(sheet.getCell(1,j).getContents(),sheet.getCell(2,j).getContents(), (ChromeDriver) driver);
                }/*
                else if(value.equals("input_xpath")){
                    api.input_xpath(sheet.getCell(1,j).getContents(),sheet.getCell(2,j).getContents(),driver);
                }*/
                else if(value.equals("verifyText")){
                    test.verifyText(sheet.getCell(1,j).getContents(), (ChromeDriver) driver);
                }
                else if(value.equals("back")){
                    test.back(sheet.getCell(1,j).getContents(), (ChromeDriver) driver);
                }
                else if(value.equals("buttonClick_className")){
                    test.buttonClick_className(sheet.getCell(1,j).getContents(),(ChromeDriver)driver);
                }
                else if(value.equals("buttonClick_linkText")) {
                    test.buttonClick_linkText(sheet.getCell(1, j).getContents(), (ChromeDriver) driver);
                }
                else{
                    //if reached the end of the file, break out the loop
                    break;
                }
            }
        }catch (BiffException e) {
            e.printStackTrace();
        }
        finally{
            driver.quit();
        }

    }

}

执行后的产生的报告,刻意写错一个元素。为了去验证下判断是否正确,结果如我所愿,这个元素没有找到,说明代码判断没问题,哈哈

42b4e888eabbf0858abf4aa3a023e85d075.jpg

data 表

507808fdfdb312138e1b7be90803244b2b3.jpg

代码执行成功,报告也没有问题;在操作过程中遇到的小问题:

1.我直接把numbers存储为excel格式结果执行时报错,这个提示也很清奇,说是创建session失败;查了下说是我excel的格式必须为.xls,而number导出为excel时默认为.xlsx,然后我就直接修改了后缀名,结果仍然报错没用;后来重新导出,95fee659fda0ba5c3f8ddb23e092d1cfdb3.jpg选择高级选项,兼容版本,再执行-成功;

2.那个等待时间我写的是让线程死等,这个方式感觉很冒傻气,网上查的代码不太懂,后期我再研究优化下

转载于:https://my.oschina.net/EvanDev/blog/1829626

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值