Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同时测试浏览器;接收误差_Java_Xml_Maven_Selenium_Testng - Fatal编程技术网

Java 同时测试浏览器;接收误差

Java 同时测试浏览器;接收误差,java,xml,maven,selenium,testng,Java,Xml,Maven,Selenium,Testng,现在,我正在尝试进行我的第一个测试,它将同时测试浏览器Chrome、Firefox、IE和Safari。但我得到的以下错误是: 配置失败:@BeforeMethod BeforeMethod org.testng.TestNGException: 参数“browser”是@Configuration on method beforeMethod>所必需的,但未标记为@Optional或已定义 我在JAVA语言中使用Selenium、TestNG和Maven。XML测试套件文件和java文件位于目

现在,我正在尝试进行我的第一个测试,它将同时测试浏览器Chrome、Firefox、IE和Safari。但我得到的以下错误是:

配置失败:@BeforeMethod BeforeMethod org.testng.TestNGException: 参数“browser”是@Configuration on method beforeMethod>所必需的,但未标记为@Optional或已定义

我在JAVA语言中使用Selenium、TestNG和Maven。XML测试套件文件和java文件位于目录中的同一文件夹中。我在网上找到的测试套件XML文件如下(类名值设置为正确的包和类名):


如果有人能告诉我是什么导致了这个问题,我将不胜感激。谢谢。

看起来您缺少pom.xml中的testNG配置

<build>
    <plugins>
        <!-- Following plugin executes the testng tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <!-- Suite testng xml file to consider for test execution -->
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/java/com/sqa/ts/multiBrowser/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
</build>

org.apache.maven.plugins
maven surefire插件
2.19
src/test/java/com/sqa/ts/multiBrowser/testng.xml

运行mvn clean installmvn clean install应运行测试用例。希望这对你有帮助

它看起来像是以runas>Testng套件的形式执行XML文件。这就是我的问题的解决方法。

它与option=Run as>Testng Suite一起工作

将@Parameters(“浏览器”)放在@BeforeMethod之前是否解决了您的问题?上述两个文件似乎完全没有问题,因为我复制了这些文件,就像在maven设置中一样。它正在同时调用Chrome,IE,Firefox。你是如何运行测试的?只有当您
以TestNG Test运行时,才会收到上述错误。尝试将其作为
TestNG套件
Maven Test
运行。谢谢@mk08,运行TestNG套件使测试开始工作。:)@太好了!请检查下面Sigil的答案。如果你想让它通过Maven运行所有的东西,你最终会需要它!我要试试这个。我不知道您可以在pom.xml文件中执行类似的操作。我想你真的可以更新依赖项或库。Maven是一个有很多插件的构建工具。。。它不仅仅用于依赖关系管理。
package com.sqa.ts.multiBrowser;

import java.net.MalformedURLException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserTest {

  private WebDriver driver;

  @Test
  public void testCaseOne() {
    driver.get("http://www.google.com");
    driver.close();
  } 

  @BeforeMethod
  @Parameters("browser")
  public void beforeMethod(String browser) throws MalformedURLException {
    if (browser.equalsIgnoreCase("chrome")) {
        System.setProperty("webdriver.chrome.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/chromedriver.exe");
        driver = new ChromeDriver();

    } else if (browser.equalsIgnoreCase("firefox")) {
        driver = new FirefoxDriver();

    } else if (browser.equalsIgnoreCase("ie")) {
        System.setProperty("webdriver.ie.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/IEDriverServer.exe");
        driver = new InternetExplorerDriver();

    } else if (browser.equalsIgnoreCase("safari")) {
        driver = new SafariDriver();
    }
}

  @AfterMethod
  public void afterMethod() {
    driver.quit();
  }

}
<build>
    <plugins>
        <!-- Following plugin executes the testng tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <!-- Suite testng xml file to consider for test execution -->
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/java/com/sqa/ts/multiBrowser/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
</build>