Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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);
}  
share|improve this question
    
Ok, so we know the context. But what problem are you encountering? –  dzieciou Jun 19 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 –  sathiya Jun 20 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 at 9:16

1 Answer 1

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();
share|improve this answer
1  
Thanks. It worked. –  sathiya Jun 20 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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