zoukankan      html  css  js  c++  java
  • selenium3+java+POM 跨浏览器测试之------读取配置文件

          我们知道,web 测试的时候是需要切换不同的浏览器以查看每个功能在不同浏览器上的运行情况,使得开发的程序更具有健壮性。本文先总结一下如何通过读取配置文件来切换浏览器。

          具体步骤如下:

         一、编写配置文件,写好浏览器的类型和测试的服务器的地址,方便切换。本文以火狐、谷歌、IE为例。(先注释掉谷歌和IE,需要测试的时候再注释回来)

      

       二、编写引擎类配置文件写好了之后就是编写一个浏览器引擎类,通过条件语句来读取配置文件以便控制启动不同的浏览器。

    package first;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
     
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.ie.InternetExplorerDriver;
     
    public class BrowserEngine {
        
        public String browserName;
        public String serverURL;
        public WebDriver driver;
        
        //初始化配置数据
        public void initConfigData() throws IOException{
            
            Properties p = new Properties();
            // 加载配置文件
            InputStream ips = new FileInputStream(".\TestConfig\configure2.properties");
            p.load(ips);
            
            //纯Java方法输出日志
            Logger.Output(LogType.LogTypeName.INFO, "Start to select browser name from properties file");
            //选择浏览器引擎
            browserName=p.getProperty("browserName");
            Logger.Output(LogType.LogTypeName.INFO, "Your had select test browser type is: "+ browserName);
            //选择测试地址
            serverURL = p.getProperty("URL");
            Logger.Output(LogType.LogTypeName.INFO, "The test server URL is: "+ serverURL);
            ips.close();
            
        }
        //设置条件,选择浏览器
        public WebDriver getBrowser(){
            
            if(browserName.equalsIgnoreCase("Firefox")){
                
                driver = createFireFoxDriver();
                
                Logger.Output(LogType.LogTypeName.INFO, "Launching Firefox ...");
                
            }else if(browserName.equalsIgnoreCase("Chrome")){
                
                driver= new ChromeDriver();
                Logger.Output(LogType.LogTypeName.INFO, "Launching Chrome ...");
                
            }else if(browserName.equalsIgnoreCase("IE")){
                
                driver= new InternetExplorerDriver();
                Logger.Output(LogType.LogTypeName.INFO, "Launching IE ...");
            }
            
            driver.get(serverURL);
            Logger.Output(LogType.LogTypeName.INFO, "Open URL: "+ serverURL);
            driver.manage().window().maximize();
            Logger.Output(LogType.LogTypeName.INFO, "Maximize browser...");
            callWait(5);
            return driver;
        }
        
        
         // 关闭浏览器并退出方法
        public void tearDown() throws InterruptedException{
            Logger.Output(LogType.LogTypeName.INFO, "Closing browser...");
            driver.quit();
            Thread.sleep(3000);
        }
        
        // 隐式时间等待方法
        
        public void callWait(int time){
            driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
            Logger.Output(LogType.LogTypeName.INFO, "Wait for "+time+" seconds.");
        }
        
        

        private WebDriver createFireFoxDriver() {
            WebDriver driver = null;
            FirefoxProfile firefoxProfile = new FirefoxProfile();
     
            firefoxProfile.setPreference("prefs.converted-to-utf8", true);
            
            firefoxProfile.setPreference("browser.download.folderList", 2);
            firefoxProfile.setPreference("browser.download.dir", ".\TestDownload");
            
            try {
                    driver = new FirefoxDriver();
            } catch (Exception e) {
                Logger.Output(LogType.LogTypeName.ERROR, e.getMessage());
                Logger.Output(LogType.LogTypeName.ERROR, "Failed to initilize the Firefox driver");
            }
            return driver;
        }
    }

     三、测试脚本调用浏览器引擎类实例,得到driver,开始测试自动化脚本

     四、利用TestNG编写一个测试类文件,测试切换不同浏览器是否脚本运行成功。

    package Test;
    import java.io.IOException;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
     
    import first.BrowserEngine;
     
    public class TestLaunchBrowser {
        
        public WebDriver driver;
        
        @BeforeClass
        public void setUp() throws IOException{
            
            BrowserEngine browserEngine = new BrowserEngine();
            browserEngine.initConfigData();
            driver=browserEngine.getBrowser();
            
        }
        
        
        @Test
        public void clickNews() throws InterruptedException{
            
            driver.findElement(By.id("key")).sendKeys("iPhone 7");
            driver.findElement(By.xpath("//*[@id='search']/div/div[2]/button")).click();
            Thread.sleep(2000);
            
        }
        
        @AfterClass
        public void tearDown(){
            
            driver.quit();
        }
     
     
    }

     注意:这里要强调的就是setUp方法中,如何获取浏览器driver这个实例对象并赋值给当前测试脚本中定义的driver,这个一定要理解好。一句话解释就是,你在浏览器引擎类定义了一个driver对象,在测试脚本中又定义了一个driver对象,你需要保持整个测试过程,只有一个唯一的driver,否则会报错,测试脚本不会执行查找元素和点击事件。 

  • 相关阅读:
    MathType输入框怎么调整
    几何画板中去除画出的线段的教程
    MathType怎么编辑半开半闭区间
    几何画板给月牙图形填充颜色的技巧
    MathType调整矩阵分隔线粗细的方法
    帮你深入理解OAuth2.0协议
    phalapi
    Spring松耦合实例
    让前端工程师糟心的正则表达式到底是什么?
    composer安装
  • 原文地址:https://www.cnblogs.com/miaojjblog/p/9692562.html
Copyright © 2011-2022 走看看