Selenium2 - driver的初始化

本文详细介绍了一个自动化测试框架的搭建过程,包括配置文件读取、浏览器初始化、远程WebDriver使用及异常处理。通过示例代码展示了如何根据不同的语言设置,初始化特定的数据和GUI组件,以及如何根据配置启动本地或远程WebDriver。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;

 

public class HelperClass
{

    private String fileName;
    private String browser;
    private String chromeDriverPath;
    private String ieDriverPath;
    private WebDriver driver;
    private Properties prop;
    private String serverHost;
    private String slowdownSeconds;
    private String implicitlyWaitTimeOut;
    private String implicitlyWaitForSpecialTimeOut;
    private String testingSiteURL;
    private String loginUser;
    private String loginPwd;
    private CVData cvData;
    private CVGUI cvGui;
    private String myTestStr;
    private String randomNum;

    /**
     *
     */
    public HelperClass() {
        // TODO Auto-generated constructor stub
        super();
    }

    public void setup()
    {
        //super.setup();

        InputStream propFile;

        fileName = new String("test.properties");

        prop = new Properties();

        try
        {
            propFile = new FileInputStream("C:/automation/tests/auto_proj/" + fileName);  
            prop.load(propFile);

            this.serverHost = prop.getProperty("serverHost");

            this.browser = prop.getProperty("browser");
            System.out.println("The browser is: " + browser + "\n");
            
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            System.out.println(dateFormat.format(cal.getTime()) + "\n");            

            this.chromeDriverPath = prop.getProperty("chromeDriverPath");
            this.ieDriverPath = prop.getProperty("ieDriverPath");

            this.implicitlyWaitTimeOut = prop.getProperty("implicitlyWaitTimeOut");
            this.implicitlyWaitForSpecialTimeOut = prop.getProperty("implicitlyWaitForSpecialTimeOut");
            System.out.println("The implicitlyWaitTimeOut is: " + this.implicitlyWaitTimeOut + " seconds\n");
            System.out.println("The implicitlyWaitForSpecialTimeOut is: " + this.implicitlyWaitForSpecialTimeOut + " mili-seconds\n");

            propFile.close();

        }
        catch (IOException e)
        {
            System.out.println("[ERROR]: Failed loading property file.");
            System.out.println(e.toString());
            System.exit(1);
        }
        
        try
        {
            this.testDataAndGuiInit();
        }
        catch (Exception e)
        {
            System.out.println("\n[ERROR]: Failed initializing test data and gui.\n");
            System.exit(1);
        }
        
        System.out.println("CVData.myStr is: " + CVData.myStr + "\n");
        System.out.println("CVGUI.myGuiStr is: " + CVGUI.myGuiStr + "\n");        

        try
        {
             if (!this.serverHost.equalsIgnoreCase("none"))
             {
                 if (this.browser.equalsIgnoreCase("chrome"))
                 {
                     driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.chrome());
                 }
                 else if (this.browser.equalsIgnoreCase("ie"))
                 {
                     driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.internetExplorer());
                 }
                 else if (this.browser.equalsIgnoreCase("ff"))
                 {
                     driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.firefox());
                 }
                
                 System.out.println("The serverHost running remote is: " + prop.getProperty("serverHost") + "\n");
             }
             else
             {
                 if (this.browser.equalsIgnoreCase("chrome"))
                 {     //我们需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为.                                                     
                        ChromeOptions options=new ChromeOptions();
                        Map<String, Object> prefs=new HashMap<String, Object>();
                        prefs.put("profile.default_content_settings.popups", 0);  //禁止弹出窗口
                        prefs.put("download.default_directory", "C:/automation");  //设置下载路径
                        System.setProperty("webdriver.chrome.driver", this.chromeDriverPath);
                        DesiredCapabilities cap=DesiredCapabilities.chrome();
                        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
                        cap.setCapability(ChromeOptions.CAPABILITY, options);

                        System.out.println("The System Propery is: " + System.getProperty("webdriver.chrome.driver").toString() + "\n");
                        driver=new BaseChromeDriver(cap);
                        
                 }
                 else if (this.browser.equalsIgnoreCase("ie"))
                 {
                         System.setProperty("webdriver.ie.driver", this.ieDriverPath);

                         System.out.println("The System Propery is: " + System.getProperty("webdriver.ie.driver").toString() + "\n");

                         driver = new BaseInternetExplorerDriver();
                 }
                 else if (this.browser.equalsIgnoreCase("ff"))
                 {
                         driver = new BaseFirefoxDriver();
                 }
                        
                
                 System.out.println("The server is running local\n");
             }

             driver.manage().timeouts().implicitlyWait(this.getImplicitlyWaitTimeOut(),  TimeUnit.SECONDS);
        }
        catch (Exception e)
        {
            System.out.println("[ERROR]: Faile creating driver.");
            System.out.println(e.toString());
            System.exit(1);
        }
    }

 

public void testDataAndGuiInit()
    {
        String lang = prop.getProperty("language");
        
        if (lang == null)
        {
            System.out.println("\n\n\n\n\n[ERROR]: Please set language in test.properties first, thanks!\n\n\n\n\n");
            System.exit(1);
        }
        
        System.out.println("The current language setting is: " + lang + "\n");
        
        if (lang.equalsIgnoreCase("Eng"))
        {
            cvData = new CVData();             //default English
            cvGui = new CVGUI();            //default English
        }
        else if (lang.equalsIgnoreCase("Sch"))
        {
            cvData = new CVData("Sch");        //Simplified Chinese
            cvGui = new CVGUI("Sch");        //Simplified Chinese
        }
        else
        {
            //Leave the option for the future
        }
    }

    public WebDriver getWebDriver()
    {
        return driver;
    }

========================================================

 

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

 

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class TestV{

@BeforeClass
    public static void setUpBeforeClass() throws Exception {
        helper = new HelperClass();
        helper.setup();
        driver = helper.getWebDriver();
        driver.manage().window().maximize();
        driver.get(“https://www.bvtest.com”);
      ....      
    }
    

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        driver.quit();
    }
   

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值