Java 使用参数时,Testng忽略@Test

Java 使用参数时,Testng忽略@Test,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,我正在尝试执行一个简单的测试,其中firefox浏览器和chrome浏览器同时打开,它们都获取相同的URL。但我的@Test每次都被忽略了。@BeforeClass似乎很好用。谁能帮我一下吗?提前感谢 这是我的密码: public class main { WebDriver driver; @BeforeClass // this will perform before your test script @Parameters({"browser"}) // Here it will pi

我正在尝试执行一个简单的测试,其中firefox浏览器和chrome浏览器同时打开,它们都获取相同的URL。但我的@Test每次都被忽略了。@BeforeClass似乎很好用。谁能帮我一下吗?提前感谢

这是我的密码:

public class main {
WebDriver driver;

@BeforeClass  // this will perform before your test script
@Parameters({"browser"}) // Here it will pickup the parameters given in XML file
public void beforeTest(String browser){
    if(browser.equalsIgnoreCase("chrome")) {
        System.out.println("Chrome");
        System.setProperty("webdriver.chrome.driver", "D:/chromedriver_32bit_forChrome76/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    } else if(browser.equalsIgnoreCase("Firefox")){
        System.out.println("Firefox");
        System.setProperty("webdriver.gecko.driver","D:/geckodriver_64/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }
}

@Test (alwaysRun = true)
public void setBaseUrl(WebDriver driver) throws InterruptedException {
    //For both the browsers
    System.out.println("Inside the Test");
    driver.get("https://google.com");
}

  @AfterClass // this will quit your browser after execution
public void afterTest() throws Exception{
    Thread.sleep(10);
    driver.quit();
}}
对应的testng.xml文件是:

<suite name="SmokeTest">

   <test name="setBaseUrlFirefox">

      <parameter name="browser" value="firefox"/>

      <classes>

         <class name="Products.main"/>

      </classes>

   </test> <!-- Test -->

   <test name="setBaseUrlChrome">

       <parameter name="browser" value="chrome"/>

       <classes>

          <class name="Products.main"/>

       </classes>

   </test> <!-- Test -->

为什么您要在公共空间中传递驱动程序挫折UrlWebDriver驱动程序?
尝试使用@BeforeTest注释,而不是@BeforeClass和您的setBaseUrl(不带参数),这样每个测试将使用不同的WebDriver,因为您使用的是局部变量,而不是实例。我重新命名了变量以使其更清晰。驱动器全局和驱动器局部

基本上,您必须从过程setBaseUrl中删除输入条件

WebDriver driverglobal;  

@Parameters({"browser"}) // Here it will pickup the parameters given in XML file public void beforeTest(String browser){
    if(browser.equalsIgnoreCase("chrome")) {
        //code ...
        driverglobal = new ChromeDriver();
        driverglobal.manage().window().maximize();
    } else if(browser.equalsIgnoreCase("Firefox")){
       //code ...
        driverglobal = new FirefoxDriver();
        driverglobal.manage().window().maximize();
    } 
}


@Test (alwaysRun = true)  
public void setBaseUrl(WebDriver driverlocal) throws InterruptedException {
        //For both the browsers
        System.out.println("Inside the Test");
        driverlocal.get("https://google.com"); }