【问题标题】:Selenium - TestNG Sequence in individual test vs testsuiteSelenium - 单个测试与测试套件中的 TestNG 序列
【发布时间】:2021-02-11 09:08:00
【问题描述】:

我在 Eclipse IDE 的 maven 中开发了一个 POM 测试框架。目前我有 3 个测试文件。每个测试文件都有

1)constructor - 调用 Base 类方法来设置和启动浏览器。 2) @Test - 实际测试 3) @AfterClass - 进行测试后清理。退出驱动,关闭文件连接。

当我单独运行它们时,它们运行良好。但是,当我通过 testng.xml 作为套件运行它们时,它给我带来了麻烦。它 1)依次启动所有浏览器 2)然后运行测试,从一个测试到另一个测试的控制。 3) 只退出一个浏览器。

我希望我在测试套件中的执行顺序如下所示。

  1. 为测试文件 1 启动浏览器
  2. 运行测试 3)退出浏览器 4)为测试文件2启动浏览器
  3. 运行测试 6)退出浏览器等。

我不知道我哪里出错了。请建议。以下是我的基类中的代码。

/**
 * 
 */
package seleniumeasy.qa.Base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import seleniumeasy.qa.Util.commonUtil;

/**
 * @author sanee
 *
 */
public class Base {
    
    protected static WebDriver driver=null;
    private static FileInputStream fis;
    private static Properties prop;
    
    public Base()
    {
        System.out.println("I am in Base constructor");
        try {
            fis = new FileInputStream(commonUtil.sConfigPath);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        prop = new Properties();
        try {
            prop.load(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public void Init()
    {
        System.out.println("I am inside Init.");
        
        //if(driver==null)
        //{
            System.out.println("I am inside If:");
            if(prop.getProperty("browser").equalsIgnoreCase("chrome"))
            {
                System.setProperty(prop.getProperty("chromekey"),prop.getProperty("chromepath"));
                driver = new ChromeDriver();            
            }
            if(prop.getProperty("browser").equalsIgnoreCase("edge"))
            {
                System.setProperty(prop.getProperty("edgekey"),prop.getProperty("edgepath"));
                driver = new EdgeDriver();          
            }
            if(prop.getProperty("browser").equalsIgnoreCase("ff"))
            {
                System.setProperty(prop.getProperty("ffkey"),prop.getProperty("ffpath"));
                driver = new FirefoxDriver();           
            }
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
    
            driver.manage().timeouts().implicitlyWait(commonUtil.iImplicitWait, TimeUnit.SECONDS);
            driver.manage().window().maximize();        
            driver.get(prop.getProperty("url"));
            
            
            /*Alert alt = driver.switchTo().alert();
            alt.dismiss();*/
            
            
            driver.findElement(By.linkText("No, thanks!")).click();
            
        }       
        
        
    //}
    
    public void postCleanUp(String sTestName)
    {
        System.out.println("I am in postcleanup from: + " + sTestName);
        driver.close();
        driver.quit();
        driver=null;
        /*try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        
    }
}

以下是我的一个测试类中的代码

package seleniumeasy.test.Tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import seleniumeasy.qa.Base.Base;
import seleniumeasy.qa.Page.HomePage;
import seleniumeasy.qa.Page.tblPaginationPage;

public class HomePageTest extends Base 
{

    public HomePage obj;
    public tblPaginationPage tblObj;
    SoftAssert sAssert;
    
    
    
    public HomePageTest()
    {
        Init();
        obj = new HomePage();
        sAssert = new SoftAssert();
        
    }
    
    @Test
    public void verifyTablePaginationMenu()
    {
        Reporter.log("Test Name is: verifyTablePaginationMenu");
        tblObj = obj.clickTablePagination();
        WebElement sPageTitle = driver.findElement(By.tagName("h2"));
        
        sAssert.assertEquals("Table with Pagination Example", sPageTitle.getText());
        
    }
    @AfterMethod    
    public void sAssertAll()
    {
        System.out.println("I am in HomePage AfterClass");
        //System.out.println("I came here");
        postCleanUp("HomePageTest");
        sAssert.assertAll();
        
    }
    /*@AfterClass
    public void closeConnection()
    {
        //postCleanUp();
    }*/
}

以下是testng.xml文件中的代码

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
  
<suite name="AutomationSuit" verbose="1" >
  
  
    <listeners>
        <listener class-name="seleniumeasy.qa.Util.TestListener"/>
    </listeners>
  <test name="Regression">
    <classes>
      <class name="seleniumeasy.test.Tests.HomePageTest"/>
      <class name="seleniumeasy.test.Tests.TablePaginationTest"/>
      <class name="seleniumeasy.test.Tests.tblDataSearchTest"/>
    </classes>
  </test>
</suite>

请找到我的 POM 框架文件夹层次结构的附件图片以便更好地理解。

【问题讨论】:

    标签: java selenium maven annotations testng


    【解决方案1】:

    我通过在 BeforeMethod 注释中添加 Init() 和在 AfterMethod 注释中添加 postcleanup() 解决了我的问题。

    【讨论】:

      【解决方案2】:

      对于您调用 Init() 的每个类,这就是您拥有三个新浏览器实例的原因。 当你打电话时:

      public void postCleanUp(String sTestName)
      

      尝试通过 webdriver

      public void postCleanUp(String sTestName, WebDriver driver1)
      

      然后做 driver1.quit()

      【讨论】:

      • 嗨,不,它不起作用。输出与之前相同。一个浏览器关闭,但其他两个仍然存在,然后它不知道要使用哪个浏览器。
      • 我通过在 BeforeMethod 注释中添加 Init() 和在 AfterMethod 注释中添加 postcleanup() 解决了我的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 2012-03-07
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多