0

I have multiple tests in one class in Selenium TestNG. Problem is that nearly every time I get some timeout exception in different test or in a different step in a test. So is hard to catch exact element causing the timeout.

org.openqa.selenium.ElementNotVisibleException: element not visible
OR
org.openqa.selenium.WebDriverException: unknown error: Element <img data-aura-rendered-by="3:3231;a" src="avatar_96.png" class="profileTrigger branding-user-profile circular" alt="User" title="User"> is not clickable at point (1887, 68). Other element would receive the click: <div class="panel slds-modal slds-modal--large slds-fade-in-open" aria-labelledby="title_1233:319;a" tabindex="-1" role="dialog" aria-modal="true" data-aura-rendered-by="1249:319;a" style="opacity: 1;">...</div>

What is the best way to avoid timeout in Selenium TestNG when I have multiple tests in one class? Is there some code which I can put for instance to

@BeforeClass or @Beforetest

Which will apply for all tests in the class?

For a better understanding, I am sharing my code.

 public class BothApprovalCockpitLighting   {

  private static WebDriver driver;
  public static WebDriverWait wd;

  @BeforeTest
  public void setUp()
  {
      System.out.println("*******************");
      System.out.println("launching Chrome browser");
      System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

  }

  @BeforeClass
  public void login() throws Exception
  {
       driver.get("https://test.com/"); 

       wd = new WebDriverWait(driver, 30);
        wd.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='username']")));

        // Enter username in textbox 
        System.out.println("Enter username in textbox"); 
        driver.findElement(By.xpath("//input[@id='username']")).sendKeys("USERNAME" ); //Batcon


        //Enter Password in texbox 
        System.out.println("Enter Password in texbox"); 
        driver.findElement(By.xpath("//input[@id='password']")).sendKeys("PASSWORD");

        //Click on Login button 
        System.out.println("Click on Login button"); 
        driver.findElement(By.xpath("//input[@id='Login']")).click(); 

        System.out.println("Login Sucessfully");
  }

  @Test(priority=1)
  public void test1DontCointainApprovalCockpit() throws Exception
  {

      driver.findElement(By.xpath("//img[@alt='All Tabs']")).click();
      String pageSource = driver.getPageSource();

      boolean b1 = pageSource.contains("Approval");
      boolean b2 = pageSource.contains("Cockpit");


      System.out.println("Page should not cointain - Approval Cockpit. This is a negative test.");
      System.out.println("Page cointain Approval: " + b1);
      System.out.println("Page cointain Cockpit: " + b2);

      if(b1 || b2)
      {
          System.out.println("Test 1 is fail - Page cointain Approval Cockpit.");
      }
      else
      {
          System.out.println("Test 1 is past - Page doesn´t cointain Approval Cockpit.");
      }

      Assert.assertFalse(b1,"Failed test 1 page cointain Approval");
      Assert.assertFalse(b2,"Failed test 1 page cointain Cockpit");


  }

  @Test(priority=2)
  public void test2SwitchCointain() throws Exception
  {
      String actualText1 =  driver.findElement(By.xpath("(//a[contains(text(),'Switch')])[2]")).getAttribute("innerHTML");

      Assert.assertTrue(actualText1.contains("Switch to Lightning Experience"), "Failed test 2 page doesn´t cointain Switch");



  }

  @Test(priority=3)
  public void test3Switch() throws Exception
  {


       driver.findElement(By.xpath("//*[text()[contains(.,'John')]]")).click();
       driver.findElement(By.linkText("Switch")).click();

  }

  @Test(priority=4)
  public void test4FMIRApprovalCokpit() throws Exception
  {
      driver.findElement(By.className("slds-icon-waffle")).click();
      String actualText1 =  driver.findElement(By.xpath("//*[text()[contains(.,'FIR')]]")).getAttribute("innerHTML");



      System.out.println("FIR text: " + actualText1);



      Assert.assertTrue(actualText1.contains("FMIR"), "Failed test 4 page doesn´t cointain FMIR");

  }

  @Test(priority=5)
  public void test4ApprovalCokpit() throws Exception
  {


      String actualText2 =  driver.findElement(By.xpath("//*[text()[contains(.,'Cockpit')]]")).getAttribute("innerHTML"); 


      System.out.println("Cockpit text:" + actualText2);



      Assert.assertTrue(actualText2.contains("Approval Cockpit"), "Failed test 4 page doesn´t cointain Cockpit");



  }


  @Test(priority=6)
  public void test6Link() throws Exception
  {
      driver.findElement(By.xpath("//img[@alt='FIR']")).click();
  }

  @Test(priority=7)
  public void test7backToSalesforce() throws Exception
  {
      Thread.sleep(10000);
      driver.findElement(By.xpath("//img[@alt='User']")).click();
      driver.findElement(By.linkText("Switch to Classic")).click();
  }


  @AfterClass
  public void backToClassic() throws Exception
  {

        //driver.findElement(By.xpath("//img[@alt='User']")).click();
        //driver.findElement(By.linkText("Switch to Classic")).click(); //Not working

  }


  @AfterTest
  public void tearDown()
  {

      if(driver!=null)
      {
          System.out.println("Closing Chrome browser");
          driver.quit();
      }

  }
}
3

1 Answer 1

1

My suggestion here would be to implement a Page Object Model, and have a method that abstracts driver.findElement(By).

Then instead of calling driver.findElement directly, always call your method that is abstracting it. E.g.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

    public abstract class BasePage {
        protected WebDriver driver;
        protected WebDriverWait wait;

        public BasePage(WebDriver driver) {
            this.driver = driver;
            wait = new WebDriverWait(driver, 30);
        }

        public void clickElement(By by) {
            wait.until(ExpectedConditions.elementToBeClickable(by));
            driver.findElement(by).click();
        }
    }

For each of the pages on your website, have a concrete page that extends BasePage. Use BasePage for all your interactions through WebDriver, and you'll have a much easier time implementing "error handling" throughout your tests.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.