可以使页面具有动态效果的网站建设技术,网站模板免费吗,网站备案负责人变更,平面设计接单赚钱平台使用Selenium进行自动化测试一直是将萌芽的自动化测试人员培养为专业人员的生命线。 硒是开源的#xff0c;在全球范围内被广泛采用。 结果#xff0c;您会得到社区的大力支持。 有多种用于不同语言的框架#xff0c;这些框架提供与Selenium的绑定。 因此#xff0c;您已经… 使用Selenium进行自动化测试一直是将萌芽的自动化测试人员培养为专业人员的生命线。 硒是开源的在全球范围内被广泛采用。 结果您会得到社区的大力支持。 有多种用于不同语言的框架这些框架提供与Selenium的绑定。 因此您已经掌握了开始使用Selenium的一切。 现在进入运行第一个测试脚本以使用Selenium执行自动化测试的阶段。 如果您正在学习Selenium自动化那么这些脚本将涉及基本的测试场景。 您可以验证 一个带有Selenium自动化测试的简单登录表单 。 使用Selenium WebDriver捕获网页的屏幕截图 。 在Selenium WebDriver中使用CSS定位器的 Web元素。 设置Selenium Grid以并行执行测试用例。 生成硒测试报告 。 可能还有更多的事情可能有人希望验证因为它的目的是使用Selenium执行自动化测试。 今天我将帮助您使用Selenium进行测试自动化的基本和基本验证之一。 我将演示如何使用Selenium自动化测试来处理多个浏览器选项卡。 实际方案入门 有时您可能会遇到复杂的情况其中可能必须打开新的选项卡或窗口并在打开的选项卡/窗口上执行所需的操作。 开始时处理多个选项卡或窗口可能看起来很复杂但是一旦您知道如何处理它们它就会变得非常容易。 让我们考虑一个场景。 假设您打开了Airbnb的主页并希望在另一个选项卡中打开寄宿家庭的详细信息在打开的选项卡上执行一些操作然后切换回上一个选项卡。 那你怎么做呢 您可以在网上找到与此相关的多种解决方案。 很少有人使用sendkeys方法“ Control t”打开选项卡然后在其中定位主页的正文。 由于sendKeys与浏览器行为有关大多数情况下此方法无效。 因此打开选项卡的最佳方法是使用Robot类或JavascriptExecutor。 机器人课程可确保使用“ Control t”命令打开标签页而通过javascript执行程序您可以使用windows.open轻松打开标签页。 打开选项卡后您可以使用Action Class方法或Selenium WebDriver接口方法getWindowHandlegetWindowHandles切换到选项卡。 我将在本文中展示这两种方法。 为了打开Airbnb中的标签需要解决以下测试步骤。 打开Airbnb URL。 搜索“果阿”位置。 储存任何住宿的网址。 开启新分页 切换到新标签并启动所需的存储URL。 为了打开新标签可以使用以下Robot类代码 Robot r new Robot(); r.keyPress(KeyEvent.VK_CONTROL); r.keyPress(KeyEvent.VK_T); r.keyRelease(KeyEvent.VK_CONTROL); r.keyRelease(KeyEvent.VK_T); 上面的代码有助于使用键盘的control t命令打开选项卡。 可以使用sendKeys来执行此操作但是由于使用它的浏览器的行为它对于工作或不工作的信誉似乎是零星的。 您可以如下使用sendKeys命令来复制上述行为。 driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL “t”); 使用Window Handler方法处理Selenium中的选项卡 现在我们要做的就是使用Window Handler方法切换到此打开的选项卡。 以下代码段供您参考 import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class HandlingMultipleTabs { public static void main(String[] args) throws InterruptedException, AWTException { // TODO Auto-generated method stub System.setProperty( webdriver.chrome.driver , .\\ChromeDriver\\chromedriver.exe ); WebDriver driver new ChromeDriver(); driver.manage().timeouts().implicitlyWait( 30 , TimeUnit.SECONDS); //Navigating to airbnb driver.get( https://www.airbnb.co.in/ ); driver.manage().window().maximize(); String currentHandle driver.getWindowHandle(); //locating the location, looking for homestays driver.findElement(By.id( Koan-magic-carpet-koan-search-bar__input )).sendKeys( Goa , Keys.ENTER); //Clicking on search button driver.findElement(By.xpath( //button[typesubmit] )).click(); String urlToClickdriver.findElement(By.xpath( //div[text()Luxury Three Bedroom Apartment with Pool Jacuzzi]/ancestor::a )).getAttribute( href ); //opening the new tab Robot r new Robot(); r.keyPress(KeyEvent.VK_CONTROL); r.keyPress(KeyEvent.VK_T); r.keyRelease(KeyEvent.VK_CONTROL); r.keyRelease(KeyEvent.VK_T); //getting all the handles currently available SetString handlesdriver.getWindowHandles(); for (String actual: handles) { if (!actual.equalsIgnoreCase(currentHandle)) { //switching to the opened tab driver.switchTo().window(actual); //opening the URL saved. driver.get(urlToClick); } } } } 如果要切换回原始选项卡请使用以下命令。 driver.switchTo().defaultContent(); 现在让我们尝试使用JavascriptExecutor打开选项卡并切换到上述相同场景的选项卡。 以下是参考的代码段 import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class multipltabsonce123 { public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty( webdriver.chrome.driver , .\\ChromeDriver\\chromedriver.exe ); WebDriver driver new ChromeDriver(); driver.manage().timeouts().implicitlyWait( 30 , TimeUnit.SECONDS); //Navigating to airbnb driver.get( https://www.airbnb.co.in/ ); driver.manage().window().maximize(); String currentHandle driver.getWindowHandle(); //locating the location, looking for homestays driver.findElement(By.id( Koan-magic-carpet-koan-search-bar__input )).sendKeys( Goa , Keys.ENTER); //Clicking on search button driver.findElement(By.xpath( //button[typesubmit] )).click(); String urlToClickdriver.findElement(By.xpath( //div[text()Luxury Three Bedroom Apartment with Pool Jacuzzi]/ancestor::a )).getAttribute( href ); //opening the new tab ((JavascriptExecutor)driver).executeScript( window.open() ); //getting all the handles currently avaialbe SetString handlesdriver.getWindowHandles(); for (String actual: handles) { if (!actual.equalsIgnoreCase(currentHandle)) { //switching to the opened tab driver.switchTo().window(actual); //opening the URL saved. driver.get(urlToClick); } } } } 荣誉 您已成功使用Selenium执行了自动化测试以借助Windows Handler方法切换不同的浏览器选项卡。 现在让我们以另一种方式进行研究。 使用Action类处理Selenium中的选项卡 如上所述我们可以使用Window Handler和Action Class切换到选项卡。 下面的代码片段展示了如何使用Action类切换到标签页。 由于动作类也使用sendkey的推断因此在使用中的浏览器中它可能会起作用也可能不会起作用。 import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class HandlingMultipleTabs { public static void main(String[] args) throws InterruptedException, AWTException { // TODO Auto-generated method stub System.setProperty( webdriver.chrome.driver , .\\ChromeDriver\\chromedriver.exe ); WebDriver driver new ChromeDriver(); driver.manage().timeouts().implicitlyWait( 30 , TimeUnit.SECONDS); //Navigating to airbnb driver.get( https://www.airbnb.co.in/ ); driver.manage().window().maximize(); String currentHandle driver.getWindowHandle(); //locating the location, looking for homestays driver.findElement(By.id( Koan-magic-carpet-koan-search-bar__input )).sendKeys( Goa , Keys.ENTER); //Clicking on search button driver.findElement(By.xpath( //button[typesubmit] )).click(); String urlToClickdriver.findElement(By.xpath( //div[text()Luxury Three Bedroom Apartment with Pool Jacuzzi]/ancestor::a )).getAttribute( href ); //opening the new tab Robot r new Robot(); r.keyPress(KeyEvent.VK_CONTROL); r.keyPress(KeyEvent.VK_T); r.keyRelease(KeyEvent.VK_CONTROL); r.keyRelease(KeyEvent.VK_T); //switch using actions class Actions action new Actions(driver); action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform(); //opening the URL saved. driver.get(urlToClick); } } 就是这样 您已经使用Windows Handler方法和Action类通过Selenium自动化测试处理了多个浏览器选项卡。 现在我将讨论使用硒的最常见缺点之一。 因此我们知道Selenium WebDriver是用于自动化Web应用程序的出色开源工具。 但是WebDriver的主要难点是测试脚本的顺序执行。 作为解决方案ThoughtWorksSelenium的创始人提出了Selenium Grid以帮助用户同时并行运行多个测试用例。 这大大降低了测试构建的执行。 因此当我们使用Selenium执行自动化测试时我们有一种并行运行多个测试用例的方法。 但是它的可扩展性如何 建立自己的Selenium Grid将需要大量的CPU消耗并且维护起来很麻烦。 您希望使用Selenium执行并行执行的测试数量对计算的需求越高。 所以你可以做什么 如何使用Selenium进行大规模自动化测试 使用Selenium on Cloud执行自动化测试 基于云的Selenium Grid可以让您运行测试用例而无须设置基础架构。 您所需要的只是一个互联网连接。 我们拥有多种平台可帮助我们提供丰富的浏览器版本移动设备Android版本等。 让我们在LambdaTest Selenium Grid上执行上述测试案例。 我将展示如何在基于云的平台上打开多个选项卡以及访问LambdaTest所需的详细信息例如视频屏幕截图控制台日志等。 您需要做的就是在实例化remoteWebDriver的同时设置LambdaTest URL。 此URL是用户名访问密钥和LambdaTest集线器URL的组合。 现在您需要做的就是定义所需的平台浏览器版本和附加组件。 完成此设置过程后请使用相同的多标签脚本并在LambdaTest平台上运行它。 以下是参考代码段 import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.net.URL; import java.util.Arrays; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class HandlingMultipleTabs { public RemoteWebDriver driver null ; public String url https://www.lambdatest.com/ ; public static final String username sadhvisingh24 ; // Your LambdaTest Username public static final String auth_key abcdefghi123456789 ; // Your LambdaTest Access Key public static final String URL hub.lambdatest.com/wd/hub ; //This is the hub URL for LambdaTest BeforeClass public void setUp() { DesiredCapabilities capabilities new DesiredCapabilities(); browserName capabilities.setCapability( browserName , chrome ); capabilities.setCapability( version , 73.0 ); win10 capabilities.setCapability( platform , win10 ); // If this cap isnt specified, it will just get the any available one capabilities.setCapability( build , MultipleTabs_Lambdatest ); capabilities.setCapability( name , MultipleTabs_Lambdatest ); capabilities.setCapability( network , true ); // To enable network logs capabilities.setCapability( visual , true ); // To enable step by step screenshot capabilities.setCapability( video , true ); // To enable video recording capabilities.setCapability( console , true ); // To capture console logs try { driver new RemoteWebDriver( new URL( https:// username : auth_key URL), capabilities); } catch (Exception e) { System.out.println( Invalid grid URL e.getMessage()); } System.out.println( The setup process is completed ); } Test public void handleMultipleTabs() throws InterruptedException, AWTException { // TODO Auto-generated method stub driver.manage().timeouts().implicitlyWait( 30 , TimeUnit.SECONDS); //Navigating to airbnb driver.get( https://www.lambdatest.com ); driver.manage().window().maximize(); String currentHandle driver.getWindowHandle(); //locating the blog url String urlToClickdriver.findElement(By.xpath( //a[text()Blog] )).getAttribute( href ); //opening the new tab ((JavascriptExecutor)driver).executeScript( window.open() ); //getting all the handles currently availabe SetString handlesdriver.getWindowHandles(); for (String actual: handles) { if (!actual.equalsIgnoreCase(currentHandle)) { //switching to the opened tab driver.switchTo().window(actual); //opening the URL saved. driver.get(urlToClick); } } } AfterClass public void closeDown() { driver.quit(); } } 上面的脚本将帮助您通过零停机时间的云Selenium Grid处理Selenium中的浏览器选项卡。 您可以在LambdaTest自动化仪表板上查看这些测试的状态。 在LambdaTest上使用Selenium执行自动化测试时您可以查看视频屏幕截图控制台输出以及更多内容。 下面引用的屏幕截图 测试的控制台输出 结论 我们演示了使用Selenium进行的自动化测试以使用Action Class和Windows Handler方法处理多个选项卡。 我们开始意识到在本地运行Selenium WebDriver和Grid的痛苦点。 转向基于 LambdaTest之类的基于云的Selenium Grid可以帮助您轻松扩展因此您可以大大减少构建时间并更快地交付产品。 如果您对此主题有任何疑问请告诉我。 我将针对Selenium自动化测试的基本主题撰写更多文章以帮助您更好地成长为专业的自动化测试人员。 请继续关注更多快乐的测试 翻译自: https://www.javacodegeeks.com/2019/07/handling-multiple-browser-selenium-automation-testing.html