Skip to main content

Tabs opening in selenium -Multiple tabs via (Keyschord)/Single tab using Context click and Robot class

 



I'll be showcasing you opening multiple tabs via selenium by 2 different method

1. the below code is used for opening multiple tabs in amazon using the Command(mac)/Control(win) + T method using Keychord, you can also use send keys using Action class but this approach does not always work , due to send keys issues with browser.  

2.you have another method using "Robot class" Which is explained later steps

Let's discuss first about opening multiple tans in same browser using Keys.chord Command(mac)+T method

Below is the screenshot for Amazon and we are targeting the below links to get open in different tabs

Here is the code for you



public class MultipleTabs {

WebDriver driver;

@Test

public void test2() {

System.setProperty("Webdriver.chrome.driver","chromedriver");

WebDriver driver= new ChromeDriver();

driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

driver.get("https://www.amazon.in/");

driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

//xpath of the frame
//Multitabs.findElements(By.tagName("a")).size()) give you all the links present under the frame
//"Make money with us" for that use findelement(s) instead of findelement 

WebElement Multitabs =  driver.findElement(By.xpath("//div[@class='navFooterLinkCol navAccessibility'][3]"));

List <WebElement> links =  Multitabs.findElements(By.tagName("a"));

System.out.println(links.size());

//for iterating the loop for the links


for(int i=0;i< links.size();i++)

{

//To open links in new tab  

//Keys.command is used in MAC , for windows it would be Keys.control,Keys.Enter

String opentab = Keys.chord(Keys.COMMAND,Keys.ENTER);

// open all links one by one in new tab

    links.get(i).sendKeys(opentab);

}


}

}


You can check the result  here in eclipse

Amazon - opening multiple tabs






Opening a single tabs by doing right click using Contextclick

You can use the above code for opening single link

WebDriver driver;
@Test
public void test2() throws AWTException {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
 driver.get("https://www.google.com/");
 WebElement w = driver.findElement(By.xpath("//div[@class='navFooterLinkCol navAccessibility'][3]"));
 w.sendKeys(Keys.chord(Keys.COMMAND,Keys.ENTER));


here we are going to perform mouseover over "Acccout and list" and select "Discover your style "

then right click on the "discover your style" and opening this on a new tab











public class SingleTabs {
WebDriver driver;
@Test
public void test2() throws AWTException {
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.get("https://www.amazon.co.in");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
WebElement abc = driver.findElement(By.id("nav-link-accountList"));
Actions act = new Actions(driver);
act.moveToElement(abc).build().perform();
//getting total number of links in the  hover over window frame
WebElement ad =  driver.findElement(By.xpath("//div[@id='nav-al-container']"));
List <WebElement> links =  ad.findElements(By.tagName("a"));
System.out.println(links.size());
//19 links present
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement ac = driver.findElement(By.linkText("Discover Your Style"));
act.moveToElement(ac).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//To perform right click on the menu bar using context click and click the first option
act.contextClick(ac).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
//To make sure above function worked well
System.out.println("Open link button Clicked");
}
}

Check the code and the final screenshot after the run










Opening a single tabs by doing right click using robot class

                                              public class SingleTabs {

// Performing  Mouse hover

WebElement ac = driver.findElement(By.linkText("Baby Wish List"));
act.moveToElement(ac).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Robot bot = new Robot();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//Right click 
bot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
bot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
System.out.println("Browse button Clicked");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//selecting first option of the right click which is back

WebDriver driver;

@Test

public void test2() throws AWTException {

System.setProperty("Webdriver.chrome.driver","chromedriver");

WebDriver driver= new ChromeDriver();

driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

driver.get("https://www.amazon.co.in");


driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

WebElement abc = driver.findElement(By.id("nav-link-accountList"));

Actions act = new Actions(driver);

act.moveToElement(abc).build().perform();


// create a robot class



bot.keyPress(KeyEvent.VK_DOWN);
bot.keyRelease(KeyEvent.VK_DOWN);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("Open link button Clicked");

//To make sure above function worked well
System.out.println("Open link button Clicked");


}

}





Comments

Popular posts from this blog

Cucumber - Execution of test cases and reporting

Before going through this blog please checkout blog on   Cucumber Fundamentals Cucumber is testing tool which implements BDD(behaviour driven development).It offers a way to write tests that  anybody can understand, regardless of there technical knowledge. It users Gherkin (business readable language) which helps to  describe behaviour without going into details of implementation It's helpful for  business stakeholders who can't easily read code ( Why cucumber tool,  is  called  cucumber , I have no idea if you ask me I could have named it "Potato"(goes well with everything and easy to understand 😂) Well, According to its founder..... My wife suggested I call it  Cucumber  (for no particular reason), so that's how it got its  name . I also decided to give the Given-When-Then syntax a  name , to separate it from the  tool . That's why it's  called  Gherkin ( small variety of a cucumber that's been pickled. I...

Jmeter 5.4.1- Config Elements - Part-03

  Part-01- Installation of Jmeter and HTTP's Recorder click  here Part 02--Previous blog on Assertion Config elements in Jmeter are used to configure or modify the samplers requests made to the server. These elements are added at the same or higher level of the samplers that we want to configure  Let' start with  CSV data config As the name suggest it used to read data from CSV first we need to put data in variables and then use the variables in sampler request. create a new test plan add CSV data set config Add a Thread Group and then add Sampler "Java Request"  Create a CSV file  with some data (Name and Data) and save it  Now go to Jmeter CSs data set config browse and upload the css file create Make few more changes in place of  variable name - Name and Dept Ignore first line - True Delimeter - \t (as suggested) Now move on the Sampler-" Java Request" and rename it with header elements of CSV As we have Name and d...

Beginners tutorial -:working with JMeter in Mac and windows - Part-01

  Prequisite   you should have Java downloaded in your system with Home path set under environment variables.(as of today Java version 8 and higher are required fro jmeter ) for help check out this link Note Always run the jmeter on your secondary browser,  if you give the primary browser for proxy settings then your internet connection will be disrupted for the browser as well as system For ex if you have chrome and firefox and your primary or default browser is chrome then do all the proxy setting in firefox so it won't hamper the system Internet connection  if you have safari as your default browser in your mac os then set proxy in chrome/firefox  MAC Download jmeter from the link  here click on the hypelink under section Binaries  "Apache JMeter( 5.3 ). tgz" file  for Mac   Tar file will get downloaded Double click on the tar file to unzip  once you open the folder  got to bin and search for jmeter.sh file this is a executa...