Web自动化-Selenium自动化测试-4-编写测试用例

首先要获取Driver,这里主要采用两种浏览器

package selenium.driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.util.concurrent.TimeUnit;

public class Driver {
    public static  WebDriver driver;
    public static void setFirfoxDriver(){
        System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");//这一句适用于火狐未安装在默认路径下的情况
        System.setProperty("webdriver.gecko.driver",  "src/main/resources/driver/geckodriver.exe");//定义驱动位置,最新版火狐需要使用GeckoDriver来驱动

    }
    public static void setChromDriver(){
        System.setProperty("webdriver.chrome.driver", "src/main/resources/driver/chromedriver.exe");
    }
    public static void setIEDriver(){
        System.setProperty("webdriver.ie.driver", "src/main/resources/driver/IEDriverServer.exe");
    }
    public static WebDriver getDriver(String type){

        if(driver==null){
            switch (type){
                case "Chrome": setChromDriver();driver = new ChromeDriver();
                    break;
                case "FirFox":setFirfoxDriver();driver = new FirefoxDriver();
                    break;
                case "IE":setIEDriver();driver = new InternetExplorerDriver();
                    break;
                default: setChromDriver();driver = new ChromeDriver();//默认启动chrom

            }
            ;
        }
        /**
         * timeout
         * implicitlyWait(long time, TimeUnit unit);
         *
         * setScriptTimeout(long time, TimeUnit unit);
         *
         * pageLoadTimeout(long time, TimeUnit unit);
         */
        driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
        return  driver;
    }

}

测试driver获取:

package selenium.driver;

import org.apache.log4j.Logger;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utils.LoggerUtil;


public class DriverTest {
    Logger logger ;
    @BeforeClass
    public void setup(){
        logger= LoggerUtil.getLogger(DriverTest.class);
    }
    @Test
    public void testGetDriver() {
//       logger.info(Driver.getDriver("FirFox"));
        logger.info(Driver.getDriver("Chrome"));
    }
}

基类:

package selenium.Base;

import org.openqa.selenium.*;
import selenium.driver.Driver;

public class Base extends  Driver{
    static  WebDriver driver;

    public static void  openUrl(String url){
        /**
         * Navigation接口
         */
        driver.get(url);

    }

    public static By ByID(String id){//query
        return  By.id(id);
    }

    public static WebElement getElementByID(String id){
        return   driver.findElement(ByID(id));
    }

    public static  void sendKey(String id,String text){
       getElementByID(id).click();
       getElementByID(id).sendKeys(text);
       try {
           Thread.sleep(10);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
    }

    public static void  click(By by){
      driver.findElement(by).click();
   }

    public static  void  tearDown(){
        if(driver!=null){
            driver.close();
        }
    }

}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页