嗨,我是 selenium 的新手,我尝试将 TestNG 与 IE webdriver 一起使用,现在我无法直接在类下实例化 IE 驱动程序(不是 main 方法)。当我这样做时,我收到以下错误: 该行有多个标记 - 标记上存在语法错误,需要 FormalParameter - 标记语法错误、构造错误 - token “webdriver.ie.driver”上的语法错误,无效
如果我随后使用带有 @BeforeSuite 注释的方法,我需要将驱动程序传递给类中的每个其他测试方法。有没有一种方法可以让我绕过传递驱动程序对象。
在下面找到我正在使用的示例代码:
package FirstTestNGPackage;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTestNGclass {
@BeforeSuite
public void SetDriverPaths()
{
File IEDriver = new File("C:\\Users\\REDACTED\\Desktop\\SeleniumJars\\IE Driver\\IEDriverServerX64_2.44.0.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
WebDriver Driver = new InternetExplorerDriver();
}
@Test
public void tester()
{
Driver.findElement(By.id("keywordInput")).sendKeys("REDACTED");
Driver.findElement(By.id("keywordInput")).sendKeys(Keys.ENTER);
Assert.assertEquals(Driver.findElement(By.xpath(".//*[@id='search_results']/h1/span/strong")).getText(), "REDACTED");
}
@BeforeTest
public void RMSLogin()
{
Driver.navigate().to("REDACTED");
}
@AfterTest
public void closeBrowser()
{
Driver.quit();
}
}
最佳答案
像这样初始化你的网络驱动程序,然后你可以在每个方法中使用this.driver
使用它。
public class FirstTestNGclass {
public WebDriver driver;
@BeforeSuite
public void SetDriverPaths()
{
// ....
this.driver = new InternetExplorerDriver();
}
// ....
}
关于java - 使用 IE webdriver 进行 TestNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732668/