How to write Selenium Java Application code in IDE through main() and TestNG(如何通过 main() 和 TestNG 在 IDE 中编写 Selenium Java 应用程序代码)
问题描述
我面临以下问题在 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
是保留关键字.为 method 使用其他用户定义的名称,例如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 应用程序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过 main() 和 TestNG 在 IDE 中编写 Selenium Java 应用程序代码


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01