You might have seen an application in which clicking on a link opens another web page/application in the new tab, and performing some activity on the application that opened in the new tab.
We will see automating above activity using Selenium WebDriver. Consider below application (QA BABU Demo Page) which as "Google" link clicking on this link will open Google search page in the new tab.
Lets automate it now.
Step 1: Launch "file:///D:/html/newTabScript.html" (this is a local file) in chrome browser using Selenium Web Driver. You can download the file from here
Step 2: Get the parent window handle to String variable using getWindowHandle()
Step 3: Now click on the "Google" link, Google search page should be opened in the new tab
Step 4: Get all the window handles to a Set<String> using getWindowHandles(). Now get number of windows present Set<String>
Step 5: Remove parent window handle from the all the window handles, so that Set<> contains only new tab window only in it.
Then switch to the new tab window as shown below.
Step 6: Enter some text to Google search text field and search, if the driver switched to new tab we will be able to search
This is one of the way to switch to new tab in Selenium automation. Please check the demo video below (please watch in full screen mode).
Please check complete program below:
Thanks for reading.
#HappyLearning #HappyTesting
We will see automating above activity using Selenium WebDriver. Consider below application (QA BABU Demo Page) which as "Google" link clicking on this link will open Google search page in the new tab.
Lets automate it now.
Step 1: Launch "file:///D:/html/newTabScript.html" (this is a local file) in chrome browser using Selenium Web Driver. You can download the file from here
//set browser driver path System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe"); //instantiate ChromeDriver driver = new ChromeDriver(); //maximize the browser driver.manage().window().maximize(); //implicit wait driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //launching the application driver.get("file:///D:/html/newTabScript.html");
Step 2: Get the parent window handle to String variable using getWindowHandle()
//getting parent window String parentWindow = driver.getWindowHandle();
Step 3: Now click on the "Google" link, Google search page should be opened in the new tab
Step 4: Get all the window handles to a Set<String> using getWindowHandles(). Now get number of windows present Set<String>
//get all the windows Set<String> allWindows = driver.getWindowHandles(); //No of windows=2 System.out.println("No of windows or tabs after clicking: " + allWindows.size());
Step 5: Remove parent window handle from the all the window handles, so that Set<> contains only new tab window only in it.
Then switch to the new tab window as shown below.
//removing parent window, allWindows.remove(parentWindow); Iterator<String> ite = allWindows.iterator(); //So now Set contains only new tab window only,so switch to it driver.switchTo().window((String) ite.next());
Step 6: Enter some text to Google search text field and search, if the driver switched to new tab we will be able to search
driver.findElement(By.name("q")).sendKeys("QABABU"); driver.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@value='Google Search']")).click();
This is one of the way to switch to new tab in Selenium automation. Please check the demo video below (please watch in full screen mode).
Please check complete program below:
package Tests; import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class SwitchingToNewTab { WebDriver driver; @Test public void GoogleSearchInNewTab() throws Exception { //set browser driver path System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe"); //instantiate ChromeDriver driver = new ChromeDriver(); //maximize the browser driver.manage().window().maximize(); //implicit wait driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //launching the application driver.get("file:///D:/html/newTabScript.html"); //getting parent window String parentWindow = driver.getWindowHandle(); //Clicking on the Google link driver.findElement(By.name("link")).click(); //get all the windows Set<String> allWindows = driver.getWindowHandles(); //No of windows=2 System.out.println("No of windows or tabs after clicking: " + allWindows.size()); //removing parent window, allWindows.remove(parentWindow); Iterator<String> ite = allWindows.iterator(); //So now Set contains only new tab window only,so switch to it driver.switchTo().window((String) ite.next()); driver.findElement(By.name("q")).sendKeys("QABABU"); driver.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@value='Google Search']")).click(); driver.quit(); } }
#HappyLearning #HappyTesting
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
ReplyDelete360Digitmg marketing analytics in hyderabad
This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ReplyDeleteData-science online course in chennai
Hi buddies, it is great written piece entirely defined, continue the good work constantly.
ReplyDeleteData Science Course
It is extremely nice to see the greatest details presented in an easy and understanding manner.
ReplyDeleteData Science Training
Good post found to be very impressive while going through this post which simplifies shifting from the current tab to the next tab in a very simple manner. Thanks for sharing and keep posting such an informative content.
ReplyDelete360DigiTMG Digital Marketing Course
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work Sentiment Analysis
ReplyDeleteThis is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. cable Mesh manufacturers in Pakistan
ReplyDeleteGood Post with a lot of useful questions, keep updating the list and thanks for sharing with us.
ReplyDeleteGood know more about selenium
It is extremely nice to see the greatest details presented in an easy and understanding .
ReplyDeleteselenium language.
This comment has been removed by the author.
ReplyDeleteExcellent article, I really liked this article on software development which is indeed very helpful and knowledgeable. The information about How to switch to new tab in selenium Automation ? You provided was very helpful. I’m very grateful that I found this article at right time so that I can recommend it to others. I have been learning software engineering from Gayatri Mishra, who is also a coach and she truly is a inspiration for others, her classes was indeed helpful and useful and also learned about few software development from there.
ReplyDeleteExcellent article, I really liked this article on software development which is indeed very helpful and knowledgeable. The information about How to switch to new tab in selenium Automation ? You provided was very helpful. I’m very grateful that I found this article at right time so that I can recommend it to others. I have been learning software engineering from Gayatri Mishra, who is also a coach and she truly is a inspiration for others, her classes was indeed helpful and useful and also learned about few software development from there.
ReplyDelete