下面给出了我在Eclipse中使用selenium编写的代码。
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
class GoogleSearchTest {
@Test
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver","C:\\Users\\acer\\Downloads\\selenium\\geckodriver.exe");
WebDriver driver =new FirefoxDriver();
driver.get("https://www.google.com/");
WebElement we1 = driver.findElement(By.name("q"));
we1.sendKeys("GMAIL");
we1.sendKeys(Keys.ENTER);
WebDriverWait wait1 = new WebDriverWait(driver, 5);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[text()='E-mail - Sign in - Google Accounts']"))).click();
Thread.sleep(10000);
driver.findElement(By.id("identifierId")).sendKeys("2017cs102@stu.ucsc.cmb.ac.lk");
driver.findElement(By.xpath("//*[@id='identifierNext']/div/span/span")).click();
Thread.sleep(10000);
driver.findElement(By.name("password")).sendKeys("mmalsha425@gmail.com");
driver.findElement(By.xpath("//*[@id='passwordNext']/div/span/span")).click();
}
}
它给出了以下错误。
[TestNG]找不到测试。 没有运行任何东西
用法:[options]要运行的XML套件文件
我已经安装了TestNG插件。我可以做什么来解决这个问题??
更改主函数名。 您不应该在使用TestNG时使用它
@Test
public void test() throws InterruptedException {
System.setProperty("webdriver.gecko.driver","C:\\Users\\acer\\Downloads\\selenium\\geckodriver.exe");
WebDriver driver =new FirefoxDriver();
您不需要编写main()
方法,TestNg可以自己编写。
class GoogleSearchTest {
@Test
public void test() throws InterruptedException{
//your code here
.....
}
}
如果要从main()
调用,只需:
import org.testng.TestNG;
public class Master {
public static void main(String[] args) {
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { GoogleSearchTest.class });
testng.run();
}
}
但请注意,在GoogleSearchTest
类中,不要将public static void main
用于@test