Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

We developed a Chrome extension, and I want to test our extension with Selenium. I created a test, but the problem is that our extension opens a new tab when it's installed, and I think I get an exception from the other tab. Is it possible to switch to the active tab I'm testing? Or another option is to start with the extension disabled, then login to our website and only then enable the extension. Is it possible? Here is my code:

def login_to_webapp(self):

    self.driver.get(url='http://example.com/logout')

    self.driver.maximize_window()

    self.assertEqual(first="Web Editor", second=self.driver.title)

    action = webdriver.ActionChains(driver=self.driver)

    action.move_to_element(to_element=self.driver.find_element_by_xpath(xpath="//div[@id='header_floater']/div[@class='header_menu']/button[@class='btn_header signature_menu'][text()='My signature']"))

    action.perform()

    self.driver.find_element_by_xpath(xpath="//ul[@id='signature_menu_downlist'][@class='menu_downlist']/li[text()='Log In']").click()

    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='useremail']").send_keys("[email]")

    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='password']").send_keys("[password]")

    self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/button[@type='submit'][@class='atho-button signin_button'][text()='Sign in']").click()

The test fails with ElementNotVisibleException: Message: element not visible, because in the new tab (opened by the extension) "Log In" is not visible (I think the new tab is opened only after the command self.driver.get(url='http://example.com/logout')).

Update: I found out that the exception is not related to the extra tab, it's from our website. But I closed the extra tab with this code, according to @aberna's answer:

def close_last_tab(self):

    if (len(self.driver.window_handles) == 2):

        self.driver.switch_to.window(window_name=self.driver.window_handles[-1])

        self.driver.close()

        self.driver.switch_to.window(window_name=self.driver.window_handles[0])

After closing the extra tab, I can see my tab in the video.

1 Answer

0 votes
by (62.9k points)

Below are the steps to work with tabs in the same browser:

  1. Open a new tab using Ctrl + t

  2. By using this the driver control automatically switches to the newly opened tab

  3. Perform the required operations here.

  4. In the next step, you'd have to switch back to the old tab using Ctrl + Tab. You have to keep pressing these keys unless you reach the desired tab.

  5. Once the desired tab is reached, then perform the operations you want to perform in that tab.

Then, get the present window handle and switch to a new tab using Ctrl + t

driver.get("http://google.com");

String windowHandle = driver.getWindowHandle();

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

Check the size of the output of getWindowHandles().

Then Use: 

ArrayList tabs = new ArrayList (driver.getWindowHandles());

System.out.println(tabs.size());

driver.switchTo().window(tabs.get(0)); 

The control is now in the new tab-

 driver.get("Your application URL");

//perform other operations on new tab.

Now, you can switch to the previous tab using Ctrl + Tab:

  driver.switchTo().window(mainWindowHandle);

 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

 driver.switchTo().defaultContent();

OR

You can perform the steps using Action class too.

For navigating left to the right side:

Actions action= new Actions(driver);

action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

For navigating right to the left side:

Actions action= new Actions(driver);

action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();

To get more details to check here

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification!

Browse Categories

...