避免Java Selenium TestNG超时的最佳方法,当我在一个类中有多个测试时



我在Selenium TestNG的一个类中有多个测试。问题是,几乎每次我在不同的测试或测试的不同步骤中都遇到一些超时异常。因此很难捕获导致超时的确切元素。

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>

当我在一个类中有多个测试时,避免硒测试NG超时的最佳方法是什么?是否有一些代码可以例如

@BeforeClass or @Beforetest

哪个将适用于班级中的所有测试?

为了更好地理解,我正在分享我的代码。

 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();
      }
  }
}

我在这里的建议是实现一个页面对象模型,并有一个抽象 driver.findElement(By( 的方法。

然后,不要直接调用 driver.findElement,而是始终调用正在抽象它的方法。例如

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();
        }
    }

对于您网站上的每个页面,都有一个扩展基本页面的具体页面。 使用 BasePage 通过 WebDriver 进行所有交互,您将更轻松地在整个测试过程中实现"错误处理"。

相关内容

  • 没有找到相关文章

最新更新