Selenium 脚本因 TestNG 注释而失败 [英] Selenium script fails with TestNG annotation

查看:23
本文介绍了Selenium 脚本因 TestNG 注释而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

步骤:1. 在 Eclipse 中,我创建了一个名为ForSe"的新项目.

Steps: 1. In eclipse I've created a new project named 'ForSe'.

  1. 在src"文件夹→默认包"下,我创建了一个名为 Login.java 的类.

我在这个类中的代码是这样的:

My code inside this class is like this:

public class Login_Valid {      
    public static void main(String[] args) throws InterruptedException {                
        System.setProperty("webdriver.chrome.driver","*my path*");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("*URL of prject ForSe*");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*@id='email_address']")).sendKeys("*email address*");
        driver.findElement(By.xpath("//*[@id='password']")).sendKeys("*Password*");
        driver.findElement(By.xpath("//*[@id='login-form']/div[4]/button")).click();
    }
}

当我使用 run as java application 运行时,脚本运行得非常好.但是当我在 TestNG 中使用相同的脚本时,它会跳过/失败我的测试.

The scripts run very well when I run with run as java application. But when I use the same script with TestNG, it skips/fails my test.

创建TestNG脚本的步骤如下:

Steps taken to create TestNG script is as follows:

  1. 安装 TestNG 并在相同的 src 下创建一个名为 ForSe_TestCases.java
  2. 的新包
  3. 我的TestNG脚本如下:

  1. Installed TestNG and under same src creates a new package named ForSe_TestCases.java
  2. My TestNG scripts is as follows:

public class ForSe_TestCases 
{
    WebDriver driver;
    String url = "*project's URL*";

    @Test (priority = 0)
    public void IO_login(WebDriver driver) 
    {

        //ForSe test environment URL
        driver.navigate().to(url);

        //this is official email address of IO
        driver.findElement(By.xpath("//*[@id='email_address']")).sendKeys("*email address*");

        //this is password
        driver.findElement(By.xpath("//*[@id='password']")).sendKeys("*Password*");

        //click on submit button to login
        driver.findElement(By.xpath("//*[@id='login-form']/div[4]/button")).click();
        System.out.println("Login button pressed");
    }

    @BeforeTest
    public void setup() 
    {
        // Set property for Chrome
        System.setProperty("webdriver.chrome.driver","*my path*");
        WebDriver driver = new ChromeDriver();

        //apply implicit wait
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //maximize window
        driver.manage().window().maximize();
    }
}

我无法理解我在哪一步出错了.这是我通过 Run as TestNG

I'm unable to understand at which step I'm going wrong. This the error message I get on running my test by Run as TestNG

    [Utils] [ERROR] [Error] org.testng.TestNGException:
    Cannot inject @Test annotated Method [IO_login] with [interface org.openqa.selenium.WebDriver].

    ===============================================
    Default test
        Tests run: 1, Failures: 1, Skips: 0
    ===============================================
    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    ===============================================

推荐答案

问题在于 TestNG 的设置.以下代码解决了我的问题:

The problem was with setup of TestNG. Following code solved my problem:

@BeforeTest
public void setup() 
{
    System.setProperty("webdriver.chrome.driver", my_path);
    ChromeOptions options = new ChromeOptions();
    options.addArguments("test-type");
    options.addArguments("start-maximized");
    options.addArguments("--js-flags=--expose-gc");  
    options.addArguments("--enable-precise-memory-info"); 
    options.addArguments("--disable-popup-blocking");
    options.addArguments("--disable-default-apps");
    options.addArguments("test-type=browser");
    options.addArguments("disable-infobars");
    driver = new ChromeDriver(options);        
}

这篇关于Selenium 脚本因 TestNG 注释而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆