0

In My below code, @Test(priority=3) deals with opening a button in new tab. but i am unable to print the title. I couldn't understand the logical issue in the code. Selenium doesn't throw any error message. But still title of the new tab is not printed

package testNGbasics;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    public class Expsrchresult extends CommonVariables {
        @BeforeClass
        public void setup()
            {

            System.setProperty(key,path);
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
            }
        @Test (priority=1)
        public void setBaseURL1()
        {
            //Navigating to search results
            driver.get(baseUrl1);
            System.out.println(driver.getCurrentUrl());
        }

        @Test (priority=2)
        public void departresultlist()
        {
          driver.findElement(By.xpath("//span[@class='title-city-text']")).getText();
          driver.findElement(By.xpath("//div[@class='title-date-rtv']")).getText();
            //Printing the search results
            List<WebElement> elements = driver.findElements(By.xpath("//ul[@id='flightModuleList']"));
              for (WebElement webElement : elements) 
              {
                  String srchresult = webElement.getText();
                  System.out.println("The Departure details "+srchresult);
              }

              driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).click();

        }
        @Test(priority=3)
        public void returnclick() throws InterruptedException
        {
          driver.findElement(By.xpath("//span[@class='title-city-text']")).getText();
          driver.findElement(By.xpath("//div[@class='title-date-rtv']")).getText();
          //printing return search results
        List<WebElement> elements1 = driver.findElements(By.cssSelector("#flightModuleList"));
          for (WebElement webElement : elements1) 
          {
            String srchresult1 = webElement.getText();
            System.out.println("The Return details "+srchresult1);
        }

          driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).sendKeys(Keys.CONTROL,Keys.ENTER);
          Thread.sleep(8000L);
            Set<String> abc = driver.getWindowHandles();
            Iterator<String> it = abc.iterator();

            Thread.sleep(8000L);
            while(it.hasNext())
            {
                driver.switchTo().window(it.next());
                System.out.println(driver.getTitle());     
            }

        }

    }

Test results are mentioned below.

PASSED: setBaseURL1
PASSED: departresultlist
PASSED: returnclick

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Once you switchTo() the window through it.next() you need to induce some wait for Selenium to recognize the Title of the new TAB.

For a quick test add some sleeps:

Thread.sleep(8000L);

Ideally, you need to induce WebDriverWait for the titleContains() before collecting the Title as follows:

new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partialPagaTitle"));
System.out.println(driver.getTitle());

However, while you collect the windowHandles through getWindowHandles() before switching to the new TAB you also need to ensure two things:

  • The second tab with as an additional window have rendered properly using ExpectedConditions.numberOfWindowsToBe(2)
  • The tab you are switching to isn't the parent TAB.

So your effective code block will be:

String first_handle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).sendKeys(Keys.CONTROL,Keys.ENTER);
new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> abc = driver.getWindowHandles();
Iterator<String> itr = abc.iterator();
while(itr.hasNext()) {
    String nextWindow = itr.next();
    if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
        driver.switchTo().window(nextWindow);
        new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partialPagaTitle"));
        System.out.println(driver.getTitle());
    }
}

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352