迁移到Selenium 4:下面是发生的变化

对于希望从Selenium 3迁移到Selenium 4的工程师来说,这是一个值得注意的变化和反对意见的指南。 自从Selenium项目的负责人Simon Stewart在正式的Selenium会议期间宣布对Selenium套件(Selenium IDE,Selenium WebDriver和Selenium Grid)进行重大更改和W3C标准化后,Selenium 4的发布引起了测试界的极大兴奋。 2018年在印度班加罗尔举行。

到2020年9月初,Selenium 4的alpha版本已经发布,可以下载来探索。

Manoj Kumar已经在较高级别上解释了Selenium 4的新增功能,在本周的博客系列中,我们将为最终用户和开发者社区提供代码段,好处和可能的用例,详细讨论这些功能。 在硒之上构建工具/产品。

关键的点

理解废弃的组件和它们的替代品

计划从Selenium 3到4的迁移

Selenium是一个用于web浏览器自动化的工具集,它允许我们远程控制浏览器实例并模拟用户与浏览器的交互。在Selenium 4中有很多变化,主要是W3C标准化,这意味着Selenium可以与任何软件集成而不存在任何兼容性问题。除了W3C标准化之外,还添加了很少的新方法和废弃/替换的方法。

Selenium 4中弃用了什么?

Selenium 4中有两个过时的建议。让我们对其进行探讨。

FindsBy

FindsBy接口是org.openqa.selenium.internal程序包的一部分,该程序包具有由RemoteWebDriver类实现的findElement(By)和findElements(By)方法。 这些现在已作为Selenium 4的一部分弃用。

这是内部更改,不会影响最终用户,但是,如果您有基于Selenium API构建的产品或工具,请查看这些更改。

就像之前一样,By类可以与findElement(By)和findElements(By)一起使用。

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;

public class FindsByExample {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver");
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");

        // findElement
        WebElement serachTxtBox = driver.findElement(By.name("q"));

        //findElements
        List<WebElement> allLinks = driver.findElements(By.tagName("a"));
    }
}

Actions

Actions类是一个面向用户的API,用于模拟复杂的用户手势,如悬停、鼠标移动等。在Actions类中添加了一些新方法,以替代包org.openqa.selenium.interactions下的类

click

一个新方法click(WebElement)被添加到Actions类中,它是moveToElement(onElement).click()的替代品,用于单击一个元素。

clickAndHold

同样,clickAndHold(WebElement)用于单击元素而不释放它,并且应用作moveToElement(onElement).clickAndHold()的替代。

contextClick

contextClick(WebElement)替代了moveToElement(onElement).contextClick(),该鼠标右键单击一个元素。

doubleClick

doubleClick(WebElement)用于双击元素,并代替moveToElement(element).doubleClick()。

release

release()用于在当前鼠标位置释放按下的鼠标左键。 这是org.openqa.selenium.interactions.ButtonReleaseAction类的较早部分,现在移至Actions类。

下面的示例演示了在Selenium 3第17到33行中讨论的动作方法的使用。可以替换这些方法,如下面的代码片段所示,作为Selenium 4实现的一部分,从第37行到第53行。

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;


public class DeprecatedExamples {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver");
        ChromeDriver driver = new ChromeDriver();

 /**********************************************  Selenium 3 usage  *************************************/

        Actions act = new Actions(driver);
        WebElement toDoList= driver.findElement(By.id("toDoListBtn"));

        // click method - to click on a webElement
        act.moveToElement(toDoList).click();

        // double click - double click on a webElement
        act.moveToElement(toDoList).doubleClick();

        // Context click - Right click on a webElement
        act.moveToElement(toDoList).contextClick();

        //clickAndHold method - click and hold on a webElement without releasing
        act.moveToElement(toDoList).clickAndHold();

        // release -release the hold on a webElement
        act.moveToElement(toDoList).release();

 /**********************************************  Selenium 4 usage  *************************************/
        
        Actions act1 = new Actions(driver);
        WebElement toDoList1= driver.findElement(By.id("toDoListBtn"));

        // click method - to click on a webElement
        act1.click(toDoList);

        //clickAndHold method - click and hold on a webElement without releasing
        act1.clickAndHold(toDoList);

        // Context click - Right click on a webElement
        act1.contextClick(toDoList);

        // double click - double click on a webElement
        act1.doubleClick(toDoList);

        // release -release the hold on a webElement
        act1.release(toDoList);

    }
}

FluentWait

来自FluentWait类的方法withTimeout()和pollingEvery()已经更改。现在这两个方法都接受一个参数java.time。替换两个参数int和TimeUnit的持续时间。让我们看看它的用法。

在下面的Selenium 3示例中,我们在第6行看到,pollingEvery()方法接受两个参数int和TimeUnit,可以从包java. utir .concurrent.TimeUnit中导入。在第7行,withTimeout()也接受int和TimeUnit作为参数。

Selenium 4用持续时间替换了时间单元,下面是演示它的示例代码片段。在第12行,轮询every()方法现在只接受一个参数Duration。

Duration类可以从java中导入。时间包和方法来表示时间持续时间在纳米,millis,秒,分钟,小时,天等等。

在我们的示例中,我们使用了ofMillis(int)和ofSeconds(int)方法。

类似地,第14行withTimeout()方法接受Duration作为参数。

import java.util.concurrent.TimeUnit; // Selenium 3
import java.time.Duration; // Selenium 4

// selenium 3 usage
      FluentWait wait = new FluentWait(driver)
                            .pollingEvery(20, TimeUnit.MILLISECONDS)
                            .withTimeout(20, TimeUnit.SECONDS)
                            .ignoring(NoSuchElementException.class);

//selenium 4 usage
      FluentWait wait = new FluentWait(driver)
                            .pollingEvery(Duration.ofMillis(500))
                            .ignoring(NoSuchElementException.class)
                            .withTimeout(Duration.ofSeconds(60));


驱动程序构造函数

不推荐使用几个驱动程序构造函数。 即,将接受Capabilities对象的对象替换为接受Option的对象。

这意味着您将需要为正在使用的任何Driver类创建一个特定的Options对象,设置您的要求并将该对象传递给Driver构造函数。

每个驱动程序的示例可以在下面 ChromeDriver ChromeDriver(Capabilities)被替换为ChromeDriver(ChromeOptions)。

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeDriverExample {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) throws IOException {

        System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.setAcceptInsecureCerts(true);

        ChromeDriver driver = new ChromeDriver(options);
    }
}

SafariDriver(Capabilities) 被替换为 SafariDriver(SafariOptions).

import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;

public class SafariDriverExample {

    public static void main(String[] args) throws IOException {

        SafariOptions safariOptions = new SafariOptions();
        safariOptions.setAcceptInsecureCerts(true);

        SafariDriver safariDriver = new SafariDriver(safariOptions);
        safariDriver.get("https://www.google.com");

    }
}

EgdeDriver(Capabilities) 被替换为 EdgeDriver(EdgeOptions).

import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

public class EdgeDriverExample {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) throws IOException {

        System.setProperty("webdriver.edge.driver", PROJECT_PATH+ "/src/main/resources/EdgeDriver.exe");

        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.setAcceptInsecureCerts(true);

        EdgeDriver edgeDriver = new EdgeDriver(edgeOptions);
        edgeDriver.get("https://www.google.com");

    }
}

FirefoxDriver(Capabilities) 被替换为 FirefoxDriver(FirefoxOptions).


import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class FirefoxExample {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) throws IOException {

        System.setProperty("webdriver.gecko.driver", PROJECT_PATH+ "/src/main/resources/geckodriver");

        FirefoxOptions options = new FirefoxOptions();
        options.setAcceptInsecureCerts(true);

        FirefoxDriver driver = new FirefoxDriver(options);
        driver.get("https://www.google.com");
    }
}

InternetExplorerDriver(Capabilities) 被替换为 InternetExplorerDriver(InternetExplorerOptions).

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class InternetExplorerDriverExample {

    final static String PROJECT_PATH = System.getProperty("user.dir");

    public static void main(String[] args) throws IOException {

        System.setProperty("webdriver.ie.driver", PROJECT_PATH+ "/src/main/resources/IEDriverServer.exe");

        InternetExplorerOptions ops = new InternetExplorerOptions();
        ops.setAcceptInsecureCerts(true);

        InternetExplorerDriver ie = new InternetExplorerDriver(ops);
        ie.get("https://www.gmail.com");
    }
}

摘要

我们经历了Selenium 4中的主要弃用方法,并概述了对于最终用户从Selenium 3迁移到4至关重要的那些。

可以在Selenium WebDriver API文档中浏览所有不推荐使用的列表。 不推荐使用大量内部接口,类和ENUM,这些接口,类和ENUM对最终用户没有影响,但对于构​​建在Selenium之上的工具和产品的供应商来说可能很重要。

在本系列的下一篇文章中,我们将探讨Selenium 4中新增的功能以及它们提供的好处。

author

石头 磊哥 seven 随便叫

company

thoughtworks

大家好,本人不才,目前依旧混迹于thoughtworks,做着一名看起来像全栈的QA,兴趣爱好前端,目前是thoughtworks 西安QA社区的leader,如果有兴趣分享话题,或者想加入tw,可以找我

roles

QA(营生) dev(front-end dev 兴趣爱好)

联系方式

如果想转载或者高薪挖我 请直接联系我 哈哈

wechat:

qileiwangnan

email:

qileilove@gmail.com