【问题标题】:How to write Selenium Java Application code in IDE through main() and TestNG如何通过 main() 和 TestNG 在 IDE 中编写 Selenium Java 应用程序代码
【发布时间】:2018-06-09 15:01:18
【问题描述】:

我面临以下问题 在 Google 中搜索找不到明确的答案如何解决此问题。

错误:

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\\chro‌​medriver_win32\\chro‌​medriver.exe"); // objects and variables instantiation 
         driver = new ChromeDriver(); 
         driver.get("newtours.demoaut.com/");
    }
}

【问题讨论】:

标签: java selenium selenium-webdriver webdriver testng


【解决方案1】:

错误源于 org.apache.bcel.verifier

你必须注意以下几点:

不要使用 ChromeDriver 实现,而是使用 WebDriver 接口。 chrome 是保留关键字。为 method 使用其他用户定义的名称,例如my_function() {} 简单地定义 public void chrome() 不会执行你的Test。您必须将 public void chrome() 转换为以下任一:

  • 转换成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注解如下:

        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”org.apache.bcel.verifier.exc.AssertionViolatedException 中尝试了上述异常:发现:内部错误:糟糕!退出!!在 org.apache.bcel.verifier.exc.AssertionViolatedException.main(AssertionViolatedException.java:102) --- 面临这个问题
【解决方案2】:
System.setProperty("webdriver.chrome.driver", "chromedriver");
driver = new ChromeDriver(); 
driver.get("http://newtours.demoaut.com/");

试试这段代码,它工作正常。 我检查了这个,它运行良好。 您需要为您的网址提供 httphttps

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-30
    • 2017-12-26
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多