51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1917|回复: 0
打印 上一主题 下一主题

[原创] WebDriver启动浏览器的N种方法-自动化测试系列笔记

[复制链接]
  • TA的每日心情
    开心
    2018-8-15 14:22
  • 签到天数: 63 天

    连续签到: 2 天

    [LV.6]测试旅长

    跳转到指定楼层
    1#
    发表于 2018-1-17 10:51:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    在使用相应的WebDriver之前,应确保运行环境的OS和Browser都与WebDriver所需的运行环境相匹配。

    一、启动Firefox Browser。
    PS:由于Firefox Driver是直接打包在WebDriver Java Client Driver中,所以下载后者后,就不需要再另外下载Firefox Driver。

    (1)这种情况适用于Firefox安装在了默认路径下:
            WebDriver driver = new FirefoxDriver();//直接new一个FirefoxDriver
            Navigation navigation = driver.navigate();
            // 进入百度首页
            navigation.to("http://www.baidu.com");
    (2) 这种情况适用于Firefox未安装在默认路径下:
             System.out.println("start firefox browser...");
           System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe"); //指定firefox的安装路径
            WebDriver driver = new FirefoxDriver();
            Navigation navigation = driver.navigate();
            navigation.to("http://www.baidu.com/");

    (3)这种情况可以加载出Firefox的插件。
        首先,要知道我们为什么需要加载插件?原因是webdriver在启动浏览器时,启动的一个干净的没有任务、插件及cookies信息的浏览器(即使你本机的firefox安装了某些插件,webdriver启动firefox也是没有这些插件的),但是有可能被测系统本身需要插件或者需要调试等等,此时可以用如下方法在启动firefox时加载插件,下面示例加载firebug插件:
    import java.io.File;
    import java.io.IOException;

    import org.openqa.selenium.Alert;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriver.Navigation;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;

    public class TestDemo {

        public static void main(String[] args) {
            System.out.println("start firefox browser...");
            System.setProperty("webdriver.firefox.bin",
                    "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
            File file = new File("/files/firebug-2.0.7-fx.xpi");
            FirefoxProfile profile = new FirefoxProfile();
            try {
                profile.addExtension(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
            //active firebug extensions
            profile.setPreference("extensions.firebug.allPagesActivation", "on");   
            WebDriver driver = new FirefoxDriver(profile);
            driver.get("http://www.baidu.com");
            System.out.println("start firefox browser succeed...");
        }

    }
    ---
    上述代码并未调通,报如下异常:
    start firefox browser...
    Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7055; process output follows:
    null

    Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系统找不到指定的路径。)

    Caused by: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系统找不到指定的路径。)
       
        (4)用第(3)种情况未调通。
    每次启动如果都像上面那样在代码里面配置profile比较麻烦,可以使用下面的方法启动本机器的firefox的配置,换句话说就是我们可以事先配置本机的firefox然后用webdriver启动它,这样本机上的firefox安装了什么插件都可以直接使用了,不需要在配置profile:
        public static void main(String[] args) {
            
            System.out.println("start firefox browser...");
            System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
            ProfilesIni pi = new ProfilesIni();
            FirefoxProfile profile = pi.getProfile("default");
            WebDriver driver = new FirefoxDriver(profile);
            driver.get("http://www.baidu.com");
            System.out.println("start firefox browser succeed...");
        }


    二、启动IE Browser。

    PS:支持有三种不同的OS平台,包括Windows、Linux、Mac OS。
    (1)启动本地IE Browser。
            System.setProperty("webdriver.ie.driver",
                    "E:\\selenium\\IEDriverServer_x64_2.53.0\\IEDriverServer.exe");//IEDriverServer.exe所在本地路径
            DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
            ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            WebDriver driver = new InternetExplorerDriver(ieCapabilities);
            //进入小米首页
            driver.get("http://www.xiaomi.com");

    三、启动Chrome Browser。

    PS:只能在 Windows OS平台上运行,但要区别32bit版本和64bit版本。

    (1)启动本地Chrome Browser。
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.setProperty("webdriver.chrome.driver",
                    "E:\\chromedriver_win32\\chromedriver.exe");//chromedriver.exe所在本地路径
            WebDriver driver = new ChromeDriver();
            driver.get("http://www.xiaomi.com");
            driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.SHIFT,"webdriver"));
            driver.findElement(By.id("su")).click();
            driver.close();
        }

    朵拉note:
        public static void main(String[] args) {
            System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
            //设置DesiredCapabilities的属性包含E_ENSURE_CLEAN_SESSION以确保在Browser实例启动前清理会话的脏数据。
            DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
            capab.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
            WebDriver driver = new InternetExplorerDriver(capab);
            driver.get("http://www.xiaomi.com/");
            driver.close();
        }

    思考:如何使InternetExplorerDriver每次启动的端口不会随机变化?
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-28 16:21 , Processed in 0.064731 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表