当前位置: 首页 > 科技观察

从Selenium3升级到Selenium4需要注意什么

时间:2023-03-14 20:02:24 科技观察

一、前言在自动化技术不断发展的过程中,我们经常会遇到或者做升级自动化版本的任务,遇到各种各样的问题。对于web自动化测试,很多公司还是非常热衷于使用Selenium的。新开发的测试脚本可能都在使用Selenium4,但完成的或较旧的测试脚本可能仍然是Selenium3,甚至Selenium2。使用Selenium3升级Selenium4时,在某些情况下可能会出现一些问题。版本升级后,会有一些弃用和改动。本文总结。2、关于支持的协议Selenium4取消了对旧协议(JSONWire协议)的支持,默认使用W3CWebDriver标准(协议)。在大多数情况下,此实现对最终用户没有影响,主要的例外是Capabilities和Actions类。3.Capabilities如果测试函数的结构不符合W3C,可能会导致会话无法启动。以下为W3CWebDriver标准特性列表:browserNamebrowserVersion(replacesversion)platformName(replacesplatform)acceptInsecureCertspageLoadStrategyproxytimeoutsunhandledPromptBehavior修改前(Java):DesiredCapabilitiescaps=DesiredCapabilities.firefox();caps.setCapability("platform","Windows10";)setCapability("version","92");caps.setCapability("build",myTestBuild);caps.setCapability("name",myTestName);WebDriverdriver=newRemoteWebDriver(newURL(cloudUrl),caps);修改后(Java):FirefoxOptionsbrowserOptions=newFirefoxOptions();browserOptions.setPlatformName("Windows10");browserOptions.setBrowserVersion("92");MapcloudOptions=newHashMap<>();cloudOptions.put("build",myTestBuild);cloudOptions.put("名称",myTestName);browserOptions.setCapability("cloud:options",cloudOptions);WebDriverdriver=newRemoteWebDriver(newURL(cloudUrl),browserOptions);第四,在Java中查找元素的方法中查找元素的方法(FindsBy接口)已被删除,因为它们仅供内部使用。1、查找单个元素findElement*修改前(Java):driver.findElementByClassName("className");driver.findElementByCssSelector(".className");driver.findElementById("elementId");driver.findElementByLinkText("linkText");driver.findElementByName("elementName");driver.findElementByPartialLinkText("partialText");driver.findElementByTagName("elementTagName");driver.findElementByXPath("xPath");修改后(Java):driver.findElement(By.className("className"));driver.findElement(By.cssSelector(".className"));driver.findElement(By.id("elementId"));driver.findElement(By.linkText("linkText"));驱动程序.findElement(By.name("elementName"));driver.findElement(By.partialLinkText("partialText"));driver.findElement(By.tagName("elementTagName"));driver.findElement(By.xpath("xPath"));2、查找多个元素findElements*修改前(Java):driver.findElementsByClassName("className");driver.findElementsByCssSelector(".className");driver.findElementsById("elementId");driver.findElementsByLinkText("linkText");driver.findElementsByName("elementName");driver.findElementsByPartialLinkText("partialText");driver.findElementsByTagName("elementTagName");driver.findElementsByXPath("xPath");修改(Java):driver.findElements(By.className("className"));driver.findElements(By.cssSelector(".className"));driver.findElements(By.id("elementId"));driver.findElements(By.linkText("linkText"));driver.findElements(By.name("elementName"));driver.findElements(By.partialLinkText("partialText"));driver.findElements(By.tagName("elementTagName"));driver.findElements(By.xpath("xPath"));5.升级依赖检查安装Selenium4并升级您的项目依赖1.Java升级Selenium的过程取决于所使用的构建工具。Java中最常见的Maven和Gradle就介绍到这里。所需的最低Java版本仍然是8。(1)Maven修改前:org.seleniumhq.seleniumselenium-java3.141.59修改后:org.seleniumhq.seleniumselenium-java4.0.0进行更改后,您可以在与pom.xml文件相同的目录中执行mvncleancompile命令。(2)Gradle修改前:plugins{id'java'}group'org.example'version'1.0-SNAPSHOT'repositories{mavenCentral()}dependencies{testImplementation'org.junit.jupiter:junit-jupiter-api:5.7.0'testRuntimeOnly'org.junit.jupiter:junit-jupiter-engine:5.7.0'实现组:'org.seleniumhq.selenium',名称:'selenium-java',版本:'3.141.59'}test{useJUnitPlatform()}修改后:plugins{id'java'}group'org.example'version'1.0-SNAPSHOT'repositories{mavenCentral()}dependencies{testImplementation'org.junit.jupiter:junit-jupiter-api:5.7.0'testRuntimeOnly'org.junit.jupiter:junit-jupiter-engine:5.7.0'实现组:'org.seleniumhq.selenium',名称:'selenium-java',版本:'4.0.0'}test{useJUnitPlatform()}修改完成后,可以在build.gradle文件所在目录执行./gradlewcleanbuild命令。2.Python使用Python最重要的变化是最低版本要求。Selenium4至少需要Python3.7或更高版本。从命令行升级,可以执行:pipinstallselenium==4.0.0六、潜在的错误和deprecation消息1、Java(1)timeout中接收的wait和timeout参数已经从预期的(longtime,timeunit)到预期(持续时间)。修改前:driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);driver.manage().timeouts().setScriptTimeout(2,TimeUnit.MINUTES);driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);修改后:driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));(2)wait现在也接受不同的参数,WebDriverWait现在期望持续时间而不是秒和毫秒长的时间。FluentWait中的withTimeout和pollingEvery方法已从expected(longtime,timeunit)切换为expected(duration)。修改前:newWebDriverWait(driver,3).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#id")));Waitwait=newFluentWait(driver).withTimeout(30,TimeUnit.SECONDS).pollingEvery(5,TimeUnit.SECONDS).ignoring(NoSuchElementException.class);修改:newWebDriverWait(driver,Duration.ofSeconds(3)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#id")));Waitwait=newFluentWait(driver).withTimeout(Duration.ofSeconds(30)).pollingEvery(Duration.ofSeconds(5)).ignoring(NoSuchElementException.class);(2)合并函数不再改变调用对象一组不同的函数可以合并到另一个函数集中,这改变了调用对象。现在,需要指定合并操作的结果。修改前(因此修改了options对象):MutableCapabilitiescapabilities=newMutableCapabilities();capabilities.setCapability("platformVersion","Windows10");FirefoxOptionsoptions=newFirefoxOptions();options.setHeadless(true);选项.merge(能力);修改后(调用的结果,merge需要赋值给一个对象):(3)Firefox旧版MutableCapabilitiescapabilities=newMutableCapabilities();capabilities.setCapability("platformVersion","Windows10");FirefoxOptionsoptions=新的FirefoxOptions();options.setHeadless(true);options=options.merge(capabilities);在GeckoDriver之前,Selenium项目有一个驱动程序来自动化Firefox(版本<48)。但是,不再需要此实现,因为它在最新版本的Firefox中不起作用。为避免在升级到Selenium4时出现重大问题,setLegacy选项将显示为已弃用。建议停止使用旧的实现,只依赖GeckoDriver。以下代码将显示setLegacy,升级后已弃用。FirefoxOptionsoptions=newFirefoxOptions();options.setLegacy(true);(4)BrowserTypeBrowserType接口已经存在很长时间了,但它已经被弃用,取而代之的是新的Browser接口。修改前:MutableCapabilitiescapabilities=newMutableCapabilities();capabilities.setCapability("browserVersion","92");capabilities.setCapability("browserName",BrowserType.FIREFOX);修改后:MutableCapabilitiescapabilities=newMutableCapabilities();capabilitiessetCapability("browserVersion","92");capabilities.setCapability("browserName",Browser.FIREFOX);2.Pythonexecutable_path已弃用,请传入Service对象。在Selenium4中,需要从Service对象设置驱动程序的executable_path以防止弃用警告(或者不设置路径,而是确保所需的驱动程序在系统路径上)。修改前:fromseleniumimportwebdriveroptions=webdriver.ChromeOptions()options.add_experimental_option("excludeSwitches",["enable-automation"])options.add_experimental_option("useAutomationExtension",False)driver=webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,options=options)修改后:fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServiceasChromeServiceoptions=webdriver.ChromeOptions()options.add_experimental_option("excludeSwitches",["enable-automation"])options.add_experimental_option("useAutomationExtension",False)service=ChromeService(executable_path=CHROMEDRIVER_PATH)driver=webdriver.Chrome(service=service,options=options)