如何通过main()和TestNG在IDE中编写Selenium Java Application代码 [英] How to write Selenium Java Application code in IDE through main() and TestNG
问题描述
我正面临以下问题 在Google中搜索的内容找不到解决该问题的明确答案.
I m facing the below issue searched in Google couldn't find the clear answer how to resolve this.
错误:
org.apache.bcel.verifier.exc.AssertionViolatedException.main(AssertionViolatedException.java:102)
代码
import org.openqa.selenium.chrome.ChromeDriver;
public class Newtours
{
public static ChromeDriver driver;
public void chrome()
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\imper\\Downloads\\chromedriver_win32\\chromedriver.exe"); // objects and variables instantiation
driver = new ChromeDriver();
driver.get("newtours.demoaut.com/");
}
}
推荐答案
该错误源于 org.apache.bcel.verifier
The error is stemming out of org.apache.bcel.verifier
您必须注意以下事项:
使用 WebDriver
界面代替使用 ChromeDriver
实施.
chrome
是保留关键字.将其他一些用户定义的名称用于方法,例如my_function() {}
仅仅定义 public void chrome()不会执行您的Test
.您必须将 public void chrome()转换为以下任意一项:
Instead of using the ChromeDriver
implementation use the WebDriver
interface.
chrome
is a reserved keyword. Use some other user defined name for the method e.g. my_function() {}
Simply defining public void chrome() won't execute your Test
. You have to convert public void chrome() in to either of the following :
-
转换为
main()
函数的方法如下:
public class Newtours
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
}
}
集成TestNG
并添加 @Test
批注,如下所示:
Integrate TestNG
and add @Test
annotations as follows :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class Newtours
{
@Test
public void my_function()
{
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
}
}
这篇关于如何通过main()和TestNG在IDE中编写Selenium Java Application代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!