3

Scenario:

  1. Step-1.Click link from window-1.New window gets opened.Say it as window-2.
  2. Step-2.Do action on window-2.
  3. Step-3.Close window-2.
  4. Step-4.Move focus to window-1
  5. Step-5.Click an element in window-1(Here a New window is getting opened)
  6. Step-6.Click link from window-1 again opens another window-say it as WINDOW-2
  7. Step-7.Do actions on WINDOW-2
  8. Step-8.Close window-2
  9. Step-9.Move focus to window-1 again and perform action.
  10. Step-10.Close window-1.

---Clicking the element on parent window on step-5 opens in a new window---**

String oldTab = driver.getWindowHandle();
//clicking link to open new window
if(driver.findElement(By.xpath("element")).isDisplayed())
{ 
   Actions aBuilder = new Actions(driver);                                                            aBuilder.keyDown(Keys.SHIFT).click(link).perform();
   ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
   newTab.remove(oldTab);
   driver.switchTo().window(newTab.get(0));
   //peform action
   driver.close();
   driver.switchTo().window(oldTab);
}
String oldTab1 = driver.getWindowHandle();
driver.navigate().refresh();
//Clicking link to open in new Window
if(driver.findElement(By.xpath("element")).isDisplayed())
{ 
   Actions aBuilder1 = new Actions(driver);
   aBuilder1.keyDown(Keys.SHIFT).click(link1).perform();
   ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles());
   newTab1.remove(oldTab1);
   driver.switchTo().window(newTab1.get(0));
   //perform action
   driver.close();
   driver.switchTo().window(oldTab1);
}  
  • Ok, so we know the context. But what problem are you encountering? – dzieciou Jun 19 '14 at 19:10
  • 1
    Step-5 Clicking an element on parent window opens in a new window. I dont want it to open in new window. It should load in the same parent window – STE Jun 20 '14 at 7:45
  • Try to use keyUp() to stop pressing the shift key. If you call keyDown() from this time the key will pressed until you call keyUp() to stop this. You must call the keyUp() method for the same Actions object you called keyDown(). – Twaldigas Jun 20 '14 at 9:16
3

keyDown method presses the key but doesn't release it.


Try to replace

aBuilder1.keyDown(Keys.SHIFT).click(link1).perform();

with

aBuilder1.keyDown(Keys.SHIFT).click(link1).perform();
aBuilder1.keyUp(Keys.SHIFT).perform();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.