1

I want to open a URL on new tab by clicking on button for add account. I am working with framework, also I've found some solution but don't understand how to apply it.

Below are the code where I've find the element and return it for performing the operations. Can any one let me know with integrate the code for opening the url in new tab, using this code?

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(5, TimeUnit.SECONDS);
            if (operation.equalsIgnoreCase("Click")) {
                temp.click();

                //try to open account on another tab.
                String myWindowHandel= driver.getWindowHandle();
                driver.switchTo().window(myWindowHandel);

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

            }
            testCaseStep = true;

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

            // Take screenshot if any testcase is not working. 
            System.out.println("Taking Screen Shot");
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File("D:\\softs\\eclipse-jee-kepler-SR1-win32\\Workspace\\AutomationFramework\\Screenshot\\screenshot.jpeg")); 
        }

        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));
        }
        return temp;

    }

}
ChayanC
  • 201
  • 7
  • 29

1 Answers1

0

You can add this if condition into your operateWebDriver()

    if (operation.equalsIgnoreCase("newTab")) {
        System.out.println("newTab" + temp);
        Actions newTab = new Actions(driver);
        newTab.keyDown(Keys.COMMAND).click(temp).keyUp(Keys.COMMAND).build().perform();

        // Do your switch windows and other stuff you plan here after opening the new tab

    }

For example if you landed on http://stackoverflow.com and you want to open Questions button in a new tab which is similar to what you want to do, you can call the method like this:

        //operateWebDriver(String operation, String Locator, String value, String objectName)
        operateWebDriver("newTab", "xpath", "", "//*[@id='nav-questions']");

For more info on opening a new tab using selenium:
How to open a new tab using Selenium WebDriver and start the link?

Community
  • 1
  • 1
Chandan Nayak
  • 10,117
  • 5
  • 26
  • 36
  • In if condition we use equalsIgnoreCase("newTab"), so in sheet where I write testcases in these there is a operations column where it written "Verify", "Click", "newTab" etc, after matching this it automate the operations. Now what I want is when it click on button "Add account" then it should open the URL on new tab at same browser instead of new window. But using your given code nothing is happen, so can you please let me know the code according to my scenario? – ChayanC Jun 14 '15 at 10:56
  • 1
    if you are on windows try this, "newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();" - in short replace COMMAND to CONTROL in the method given in the answer , please try it properly. I have tested the code. it worked. – Chandan Nayak Jun 14 '15 at 11:18
  • Nayank Thanks it working fine now, new tab is opening. With this line newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform(); I switch to that window(Opened tab) but not able to resume other action in it, like I am not able to give inputs in field. So, do you have any idea after switch on window, how we can resume other operation on new opened window at other tab? – ChayanC Jun 15 '15 at 11:36