1

I am able to switch on another tab at same browser but after opening that page, on another tab I am not able to perform the operation.

Current scenario held on:

  1. Give credential and login the account.
  2. Click on email address or username through this it open a frame.
  3. From that frame click on "Add account" button.
  4. After click on "Add account" button I am successfully open the link on another tab.
  5. After opening tab, I am able to switch on that window.

But after switch on that window I want to give credential again on login page, but don't want to close the first opened tab on which account is loggedIn.

I am doing it in framework so please check the below code and according to this integrate your code and help me.

private boolean operateWebDriver(String operation, String Locator,
            String value, String objectName) throws Exception {
        boolean testCaseStep = false;

        try {
            System.out.println("Operation execution in progress");
            WebElement temp = getElement(Locator, objectName);
            if (operation.equalsIgnoreCase("SendKey")) {
                temp.sendKeys(value);
            }
            Thread.sleep(1000);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

            }
            if (operation.equalsIgnoreCase("newTab")) {
                System.out.println("newTab" + temp);
                Actions newTab = new Actions(driver);
                newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
               newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
            }
            if (operation.equalsIgnoreCase("Verify")) {
                System.out.println("Verify--->" + temp);
                temp.isDisplayed();
            }

            testCaseStep = true;
        } catch (Exception e) {
            System.out.println("Exception occurred operateWebDriver"
                    + e.getMessage());

            //Taking snapshot when test case fail.

            System.out.println("Taking snapshot");

            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));

        }

        return testCaseStep;
    }

    public WebElement getElement(String locator, String objectName)
            throws Exception {
        WebElement temp = null;
        System.out.println("Locator-->" + locator);
        if (locator.equalsIgnoreCase("id")) {
            temp = driver.findElement(By.id(objectName));

        } else if (locator.equalsIgnoreCase("xpath")) {
            temp = driver.findElement(By.xpath(objectName));
            System.out.println("xpath temp ----->" + temp);
        } else if (locator.equalsIgnoreCase("name")) {
            temp = driver.findElement(By.name(objectName));
            System.out.println("name temp ----->" + temp);
        } 
        return temp;

    }


}
ChayanC
  • 201
  • 7
  • 29

2 Answers2

0

Now I am able to work on opened URL after opening it on another tab. Here is the code which is working from my end.

private boolean operateWebDriver(String operation, String Locator,
            String value, String objectName) throws Exception {
        boolean testCaseStep = false;

        try {
            System.out.println("Operation execution in progress");
            WebElement temp = getElement(Locator, objectName);
            if (operation.equalsIgnoreCase("SendKey")) {
                temp.sendKeys(value);
            }
            Thread.sleep(1000);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

            }
            if (operation.equalsIgnoreCase("newTab")) {
                System.out.println("newTab" + temp);

                // Open link in new tab.
                Actions newTab = new Actions(driver);
                newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();

               //After Opening link in new tab display that window.
                newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();

               // Get the number of tab opened in browser.
                ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles());
                // change focus to new tab
                driver.switchTo().window(newTab1.get(0));

                // Now perform the operation here.
                if (operation.equalsIgnoreCase("SendKey")) {
                    temp.sendKeys(value);
                }
                Thread.sleep(1000);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                if (operation.equalsIgnoreCase("Click")) {
                    temp.click();

                }

            }
            if (operation.equalsIgnoreCase("Verify")) {
                System.out.println("Verify--->" + temp);
                temp.isDisplayed();
            }

            testCaseStep = true;
        } catch (Exception e) {
            System.out.println("Exception occurred operateWebDriver"
                    + e.getMessage());

            //Taking snapshot when test case fail.

            System.out.println("Taking snapshot");

            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));

        }

        return testCaseStep;
    }
ChayanC
  • 201
  • 7
  • 29
  • Glad you got something that works for you, but you're getting lucky using the first value in the list returned by `getWindowHandles()`. There is no guaranteed order to window handles. Adding logic to find the right window handle is left as an exercise to the reader; example code is available at many, many places throughout the web. – JimEvans Jun 16 '15 at 12:02
  • @JimEvans Thanks for your feedback, can you please provide me the reference URL of it? – ChayanC Jun 16 '15 at 12:35
  • How many window handles do you get, you are saying new page is opened in a new tab, if it is a tab then there will be only 1 window handle right? – skumar Jun 16 '15 at 18:33
  • @skumar Yes, currently it handle only 1 window. – ChayanC Jun 18 '15 at 07:28
0

which browser are you working on? firefox doesn't work on tab. try using chrome.. store current window handles and click the desired link to open new tab

//current window - before opening link on new tab    
String winHandleBefore = driver.getWindowHandle();  
  //write code to open new link
    tabs2 = new ArrayList<String> (driver.getWindowHandles());

        for( int k= 0 ; k< tabs2.size(); k++  ){
                for(String winHandle : driver.getWindowHandles()){
             driver.switchTo().window(tabs2.get(k));

            }
Maddy
  • 63
  • 7