[java]
WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);
//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();
set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);
//close the window
getMyDriver().close();
getMyDriver().switchTo().window(base);
// handle windows change and switch back to the main window
Thread.sleep(2500);
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}
First create a WebElement to the link. We need an Action chain to press Shift key and then click on the link which will be opened in a new tab of the same widow.
Now we have to handle the window change.
1. First set the main window to a String with getWindowHandle().
2. To get all the windows handle as a list use "getWindowsHandles()"
Now we remove the base window from the list and assert that the list size is 1.
After this we need to switch to the new window with the WebDriver:
getMyDriver().switchTo().window((String) set.toArray()[0]);
To close the window and switch back to the main window, use the following steps:
getMyDriver().close();
getMyDriver().switchTo().window(base);
Just for sure it was switched to the main window use the following window handles:
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}
Steps to open a link in a new tab and switch to it:
WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);
//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();
set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);
//close the window and sitch back to the base tab
getMyDriver().close();
getMyDriver().switchTo().window(base);
If we want to open the link in a new tab than we need to use the following action chain:
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT) .click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
It press CTRL+SHIFT keys then click to the link.
The other steps are similar with the steps we used in “switch-to-new-window” code above.
WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);
//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();
set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);
//close the window
getMyDriver().close();
getMyDriver().switchTo().window(base);
// handle windows change and switch back to the main window
Thread.sleep(2500);
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}
First create a WebElement to the link. We need an Action chain to press Shift key and then click on the link which will be opened in a new tab of the same widow.
Now we have to handle the window change.
1. First set the main window to a String with getWindowHandle().
2. To get all the windows handle as a list use "getWindowsHandles()"
Now we remove the base window from the list and assert that the list size is 1.
After this we need to switch to the new window with the WebDriver:
getMyDriver().switchTo().window((String) set.toArray()[0]);
To close the window and switch back to the main window, use the following steps:
getMyDriver().close();
getMyDriver().switchTo().window(base);
Just for sure it was switched to the main window use the following window handles:
for (String winHandle : getMyDriver().getWindowHandles()) {
getMyDriver().switchTo().window(winHandle);
}
Steps to open a link in a new tab and switch to it:
WebElement link = getMyDriver().findElement(By.xpath("//*[@id=’link’]"));
Actions newTab = new Actions(getMyDriver());
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(5000);
//handle windows change
String base = getMyDriver().getWindowHandle();
Set<String> set = getMyDriver().getWindowHandles();
set.remove(base);
assert set.size() == 1;
getMyDriver().switchTo().window((String) set.toArray()[0]);
//close the window and sitch back to the base tab
getMyDriver().close();
getMyDriver().switchTo().window(base);
If we want to open the link in a new tab than we need to use the following action chain:
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT) .click(link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
It press CTRL+SHIFT keys then click to the link.
The other steps are similar with the steps we used in “switch-to-new-window” code above.
No comments:
Post a Comment