2018-11-16容器方法调用

一 BaseUI

工具类的基本内容

  • 点击
  • 输入
  • 启动浏览器
  • 关闭浏览器
  • @Beforeclass 在所有类之前执行
  • @Afterclass 在所有类之后执行
package com.guoyasoft.autoUI.common;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BaseUI {
    //这个变量的类型是webDriver 网站驱动
    public WebDriver driver;
    public String pageTitle;
    public String pageUrl;
    
    public BaseUI(){
        
    }
    
    public BaseUI(WebDriver driver){
        this.driver=driver;
    }

    //在所有类之前执行
    @BeforeClass
    public void before(){
        //打开浏览器
        // 设置环境变量,指定chromedriver的路径
                System.setProperty("webdriver.chrome.driver",
                        "src/main/resources/selenium/driver_v236_63_65/chromedriver.exe");
                // 设置浏览器的参数
                ChromeOptions options = new ChromeOptions();
                // 最大化浏览器
                options.addArguments("--test-type", "--start-maximized");
                // options.setBinary("C:/XXXXXXX/chrome.exe");
                // 打开浏览器
                driver = new ChromeDriver(options);
                driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                sleep(1000);
    }
    
    @Test
    @Parameters({"url"})
    public void openUrl(String url){
        //打开URL
        driver.get(url);
        sleep(1000);
    }
    //在所有类执行之后执行
    @AfterClass
    public void after(){
        //关闭浏览器
        sleep(2000);
        //浏览器退出
        driver.quit();
    }
    //封装的线程等待方法
    public static void sleep(int millis) {
        try {
            Thread.sleep(millis*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //封装的截图的方法 传一个driver ,传一个图片的名字
    public static void snapshot(TakesScreenshot drivername, String filename) {
        // this method will take screen shot ,require two parameters ,one is
        // driver name, another is file name

        File scrFile = drivername.getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy
        // somewhere
        try {
            System.out.println("save snapshot path is:c:/" + filename);
            FileUtils.copyFile(scrFile, new File("c:\\" + filename));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Can't save screenshot");
            e.printStackTrace();
        } finally {
            System.out.println("screen shot finished");
        }
    }
    /*
     * 在文本框内输入字符
     */
    protected void text(WebElement element, String text) {
        try {
            if (element.isEnabled()) {
                element.clear();
                System.out.println("Clean the value if any in "
                        + element.toString() + ".");
                element.sendKeys(text);
                System.out.println("Type value is: " + text + ".");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 点击元素,这里指点击鼠标左键
     */
    protected void click(WebElement element) {

        try {
            if (element.isEnabled()) {
                element.click();
                System.out.println("Element: " + element.toString()
                        + " was clicked.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * 在文本输入框执行清除操作
     */
    protected void clean(WebElement element) {

        try {
            if (element.isEnabled()) {
                element.clear();
                System.out.println("Element " + element.toString()
                        + " was cleaned.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * 判断一个页面元素是否显示在当前页面
     */
    protected void verifyElementIsPresent(WebElement element) {

        try {
            if (element.isDisplayed()) {
                System.out.println("This Element " + element.toString().trim()
                        + " is present.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 获取页面的标题
     */
    protected String getCurrentPageTitle() {

        pageTitle = driver.getTitle();
        System.out.println("Current page title is " + pageTitle);
        return pageTitle;
    }

    /*
     * 获取页面的url
     */
    protected String getCurrentPageUrl() {

        pageUrl = driver.getCurrentUrl();
        System.out.println("Current page title is " + pageUrl);
        return pageUrl;
    }

    public void switchToNextWindow() {

        String currentWindow = driver.getWindowHandle();// 获取当前窗口句柄
        Set handles = driver.getWindowHandles();// 获取所有窗口句柄
        System.out.println("当前窗口数量: " + handles.size());
        for (String s : handles) {
            if (currentWindow.endsWith(s)) {
                continue;
            } else {
                try {
                    WebDriver window = driver.switchTo().window(s);// 切换到新窗口
                    System.out
                            .println("new page title is " + window.getTitle());
                    break;
                } catch (Exception e) {
                    System.out.println("法切换到新打开窗口" + e.getMessage());

                }
            }
        }
    }
    //根据页面标题切换窗口
    public void switchToTitleWindow(String windowTitle) {
        // 将页面上所有的windowshandle放在入set集合当中
        String currentHandle = driver.getWindowHandle();
        Set handles = driver.getWindowHandles();
        for (String s : handles) {
            driver.switchTo().window(s);
            // 判断title是否和handles当前的窗口相同
            if (driver.getTitle().contains(windowTitle)) {
                break;// 如果找到当前窗口就停止查找
            }
        }
    }
    
    
}

二 方法调用

1. 不带参

2018-11-16容器方法调用_第1张图片
 public  void  queryuser(){
      //driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(users);
      //driver.findElement(By.xpath("//input[@type='submit']")).click();

      sendkey("//input[@name='userName']","z");
      click("//input[@type='submit']");
   }

2.带参

2018-11-16容器方法调用_第2张图片
public  void  queryage(String name){
      driver.findElement(By.xpath("//input[@name='userName']")).clear();
      //driver.findElement(By.xpath("//input[@type='number'][1]")).clear();
      //driver.findElement(By.xpath("//input[@type='number'][1]")).sendKeys(age);
      driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(name);
      driver.findElement(By.xpath("//input[@type='submit']")).click();
      sleep(1000);
  }

3.调用下拉框

2018-11-16容器方法调用_第3张图片
public  void queryeducation3(String education){
    //使用findelement查找select元素 使用Webelement 对对象声明变量进行保存
        WebElement element = driver.findElement(By.xpath("//select[@name='education']"));
        //新建一个selenium 下拉框处理对象,对sele 进行处理下拉框 使用时需要传参,传参为定位的select下拉框
        Select sele= new Select(element);
        //通过自己声明的sele进行文本类型的查询
        sele.selectByVisibleText(education);
        driver.findElement(By.xpath("//input[@type='submit']")).click();
      }

三 内置批量容器类型

1. 固定长度类型 数组

2018-11-16容器方法调用_第4张图片
 public String users [] [] ={
      //先横着第几行,后竖着第几列
      {"zz1","xx1","cc1","aa1","ss1"},
      {"zz2","xx2","cc2","aa2","ss2"},
      {"zz3","xx3","cc3","aa3","ss3"},
      {"zz4","xx4","cc4","aa4","ss4"},
      {"zz5","xx5","cc5","aa5","ss5"}
  };
  @Test
  public void deome(){
    for (int i = 0; i < users.length; i++) {
      System.out.println(users[i][0]);
      System.out.println(users[i][1]);
    }
  }

2.变化长度的类型 列表

  • 只能用来存储单值


    2018-11-16容器方法调用_第5张图片
 public  void list(){
    //创建一个list列表
    List list = new ArrayList();
    //列表添加数据 list.add();
    list.add("周");
    list.add("杨");
    list.add("陈");
    list.add("周2");
    list.add("吴");
    System.out.println(list.get(3));
    System.out.println(list.get(4));
    System.out.println(list.size());
  }

3.存储键值对的类型

2018-11-16容器方法调用_第6张图片
 public  void  map(){
    //Map 用来存储键值对数据  取数据通过键值对来取
    Map jianzhidui=new HashMap();
    jianzhidui.put("周",23);
    jianzhidui.put("杨",21);
    jianzhidui.put("吴",22);
    jianzhidui.put("王",24);
    System.out.println("周的年龄是"+jianzhidui.get("周"));
    System.out.println("杨的年龄是"+jianzhidui.get("杨"));
    System.out.println("总共有多少条"+jianzhidui.size());
    }

四 maven的作用以及原理

  • 添加依赖jar包
  • 管理开发依赖jar包
  • 依赖第三方代码位置本地仓库-官方仓库
  • 官方仓库地址:http://repo1.maven.org/maven2/
  • 本地仓库(按配置找selnium和testng)

maven配置依赖

工程中使用pom文件配置依赖
maven依赖配置 pom.xml 依赖配置
依赖
org.jvnet.localizer 域名 公司名
localizer 项目名称
1.8 版本

groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。

pom配置信息

项目依赖
插件
目标
建立档案
项目版本
开发商
邮件列表

2018-11-16容器方法调用_第7张图片

你可能感兴趣的:(2018-11-16容器方法调用)