简体   繁体   English

Selenium Eclipse Test Suite测试在套件中失败,但在单独运行时成功

[英]Selenium Eclipse Test Suite tests fail in the suite, but are successful when run individually

I have the following test suite to run an automation script to login to gmail then another script to click the Compose button: 我有以下测试套件来运行自动化脚本以登录gmail,然后运行另一个脚本以单击“撰写”按钮:

TestSuite.xml TestSuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Web Admin Tests" parallel="false">
<test name="BOS - Account Class">
<classes>
<class name="secondtestngpackage.GmailComposeEmail" />
<class name="secondtestngpackage.GmailLogin" />
</classes>
</test>
</suite>

When I run this test suite, the first test does not get passed the login screen in gmail, but the second test does, even though they reference the same functions. 当我运行此测试套件时,即使第一个测试引用了相同的功能,第一个测试也没有通过gmail登录屏幕,但是第二个测试却通过了。 Is there a reason why this would happen? 有什么理由会发生这种情况吗? One test is able to enter the user id and password, since it is the second/last test, but when the first test is run, it is like the second test interferes with it, since its browser is now in focus. 一个测试可以输入用户ID和密码,因为它是第二个/最后一个测试,但是运行第一个测试时,就像第二个测试干扰了它一样,因为现在浏览器已成为焦点。

GmailLogin.java: GmailLogin.java:

 @BeforeTest
  public void launchBrowser() {
      ReuseFunctions.OpenApp("Chrome", "https://gmail.com");
  }

//Test 1:  Log into Gmail
  @Test(priority=1)
  public void LoginToGmailAccount() {

**GmailComposeEmail.java**

  @BeforeTest
  public void launchBrowser() {
      ReuseFunctions.OpenApp("Chrome", "https://gmail.com");

  }
  @Test(priority=2)
  public void LoginToGmailAccount() {

Reusable Functions File: 可重用功能文件:

ReuseFunctions.func_EnterCredentials("username", "password");

        public class ReuseFunctions {

    public static WebDriver driver; 

/*Function 1:  Select Browser and Open Application Function
*/
    public static Object OpenApp (String Browser, String URL) {
        //Receive Browser Name Function
        Func_LaunchBrowser(Browser);
        //Receive Website and Open Function
        func_OpenURL(URL);
        return driver;
    }

    //Select Browser
    public static WebDriver Func_LaunchBrowser(String Browser) {
        String driverPath = "C:/EclipseJavaDev/";
        if(Browser=="Chrome"){
            System.out.println("launching chrome browser"); 
            System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
            driver = new ChromeDriver();
            }else if(Browser=="FF"){
            driver= new FirefoxDriver();
            }else if(Browser=="IE"){
            System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
            driver= new InternetExplorerDriver();
            }
            driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
            return driver;
            }
    //Open URL
    public static void func_OpenURL(String URL){
        driver.get(URL);
        driver.manage().window().maximize();
        }

The problem lies with where your WebDriver instance is instantiated. 问题在于实例化您的WebDriver实例。 Think of each instance of WebDriver as a unique browser session. WebDriver的每个实例都视为唯一的浏览器会话。 So the safest way to ensure each test gets its own driver instance (browser session), is to initialize it at the Test class, not the common helper class. 因此,确保每个测试获得其自己的驱动程序实例(浏览器会话)的最安全方法是在Test类而不是公共帮助程序类上对其进行初始化。

GmailLogin: Gmail登录:

// Initialize Webdriver and then asks your common function to return it
private WebDriver driver;

@BeforeTest
public void launchBrowser() {
  driver = ReuseFunctions.OpenApp("Chrome", "https://gmail.com");
}

@Test(priority=1)
public void LoginToGmailAccount() {
   // pass driver as a method argument to other methods
}

//Don't forget to close the browser
@AfterTest
public void quitBrowser() {
  driver.quit();
}

Refactor your other Test class the same away as above. 与上述相同,重构其他测试类。 (Or reconsider if you really need another class. You could just add another method into the same class and use priority and/or dependsOnMethods attributes to control the execution order. But that is a different topic and out of the scope of this question :) ). (或者重新考虑是否确实需要另一个类。您可以将另一个方法添加到同一类中,并使用priority和/或dependsOnMethods 属性来控制执行顺序。但这是一个不同的主题,并且不在此问题的范围内:) )。

ReuseFunctions: 重用功能:

// Remove the global Webdriver instantiation

//Change return type from Object to WebDriver
public static WebDriver OpenApp (String Browser, String URL) {
    WebDriver driver = Func_LaunchBrowser(Browser);
    func_OpenURL(URL, driver); //pass in the driver as arg
    return driver;
}

//Refactor openUrl method to use the driver from arg
public static void func_OpenURL(String URL, WebDriver driver){...} 

This should work. 这应该工作。 Let me know how it goes. 让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果作为测试套件的一部分运行,JUnit测试将失败 - JUnit tests fail if run as part of a test suite Selenium测试在单独运行时通过,在套件中运行时挂起,并在启用日志记录或远程运行时惊人地通过套件 - A Selenium test passes when run individually, hangs when run in a suite, and amazingly passes in the suite when logging is enabled or run remotely TestNG 测试用例单独通过,但在作为测试套件运行时失败 - TestNG test cases pass individually, but fail when running as a test suite Mockito 测试单独通过但作为套件的一部分失败 - Mockito Tests Pass individually but fail as part of a suite Selenium-运行测试套件时,Eclipse:NullPointerException - Selenium - Eclipse: NullPointerException when running test suite 如何为Java Selenium测试实现Test Suite - How to implement Test Suite for Java Selenium tests 在Eclipse中的测试套件中运行单个测试 - Run single test within a test suite in Eclipse 将Selenium Test Suite(Eclipse)与TFS集成 - Integrating Selenium Test Suite (Eclipse) with TFS 作为套件运行测试时,硒找不到元素 - Selenium not finding elements when running tests as suite 多次运行同一个Selenium测试套件 - Run the same Selenium test suite multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM