使用Selenium自动化测试处理多个浏览器选项卡
使用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接口方法getWindowHandle&getWindowHandles切換到選項卡。 我將在本文中展示這兩種方法。
為了打開Airbnb中的標簽,需要解決以下測試步驟。
為了打開新標簽,可以使用以下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[@type='submit']" )).click(); String urlToClick=driver.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 Set<String> handles=driver.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[@type='submit']" )).click(); String urlToClick=driver.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 Set<String> handles=driver.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[@type='submit']" )).click(); String urlToClick=driver.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的主要難點是測試腳本的順序執行。
作為解決方案,ThoughtWorks(Selenium的創始人)提出了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 isn't 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 urlToClick=driver.findElement(By.xpath( "//a[text()='Blog']" )).getAttribute( "href" ); ????????????????//opening the new tab ((JavascriptExecutor)driver).executeScript( "window.open()" ); ????????//getting all the handles currently availabe Set<String> handles=driver.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
總結
以上是生活随笔為你收集整理的使用Selenium自动化测试处理多个浏览器选项卡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云服务器怎么防止ddos攻击(防止ddo
- 下一篇: 会计事务所从事证券业务备案流程(会计事务