在我进入它之前,我是硒的新手,依此类推。我查找了这个问题的所有答案,仍然无法解决问题。我的%claspath%等等都是正确的。 jar文件位于正确的文件夹中。我完全迷失了为什么这不起作用。我的猜测是,我正在做一些愚蠢的事情,专家会很快发现错误。
我能够在Eclipse中运行以下测试,它打开firefox并运行测试,没有任何问题。如果我从cmd或Jenkins运行测试,我会收到以下错误:错误:无法找到或加载主类org.testng.TestNG
我已将虚拟数据替换为以下实际信息:
Selenium class
package package1;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class login {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver","C:\\gecko\\geckodriver.exe");
driver = new FirefoxDriver();
baseUrl = "http://example.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testLogin() throws Exception {
driver.get(baseUrl + "index.php");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("myemail@example.ie");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("mypassword");
driver.findElement(By.xpath("//input[@value='Login »']")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
textng.xml
<suite name="SampleSuite">
<test name="LearningJenkins">
<classes>
<class name="package1.login"></class>
</classes>
</test>
</suite>
的run.bat
cd C:\Users\keating99\workspace\FdimTests
java -cp C:\Users\keating99\workspace\FdimTests\lib*;C:\Users\keating99\workspace\FdimTests\bin org.testng.TestNG testng.xml
我确信我已经完美地设置了硒测试。我检查了类路径和项目路径,一切似乎都没问题。
任何帮助都会有很大的帮助。再次提起所有其他答案,没有任何运气。在此先感谢任何人的帮助:)
更新
C:\Users\keating99\workspace\FdimTests>java org.testng.TestNG testng.xml
Exception in thread "main" java.lang.NoClassDefFoundError: bsh/EvalError
at org.testng.TestRunner.<init>(TestRunner.java:99)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:508)
at org.testng.SuiteRunner.init(SuiteRunner.java:142)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:106)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1116)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1103)
at org.testng.TestNG.runSuitesLocally(TestNG.java:955)
at org.testng.TestNG.run(TestNG.java:900)
at org.testng.TestNG.privateMain(TestNG.java:1182)
at org.testng.TestNG.main(TestNG.java:1146)
Caused by: java.lang.ClassNotFoundException: bsh.EvalError
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more
答案 0 :(得分:1)
执行 testng.xml 的最简单方法如下:
从项目目录,通过CLI提供以下类路径:
C:\Users\keating99\workspace\FdimTests>set classpath=C:\Users\keating99\workspace\FdimTests\bin;C:\Users\keating99\workspace\FdimTests\lib\*;
现在,执行 testng.xml ,如下所示:
C:\Users\keating99\workspace\FdimTests>java org.testng.TestNG testng.xml
观察Testcase是否已执行。