Selenium脚本失败,带有TestNG注释

时间:2017-03-31 05:15:17

标签: java selenium testng

步骤: 1.在日食中,我创建了一个名为“ForSe'”的新项目。

  1. 在' src'文件夹→'默认包'我创建了一个名为Login.java

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

    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();
        }
    }
    
  4. 当我使用run as java application运行时,脚本运行得非常好。 但是当我在TestNG上使用相同的脚本时,它会跳过/不通过我的测试。

    创建TestNG脚本的步骤如下:

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

      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();
          }
      }
      
    3. 我无法理解我哪一步出错了。这是我通过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
          ===============================================
      

4 个答案:

答案 0 :(得分:1)

更改代码的下方行。

public void IO_login(WebDriver driver)public void IO_login()

然后尝试运行您的代码,它肯定会对您有用。有关详细信息,请参阅以下代码。

public class ForSe_TestCases {

    WebDriver driver;
    String url = "http://google.com";

  @Test (priority = 0)
  public void IO_login() 
  {
       driver.navigate().to(url);
       System.out.println("Login Method");
  }

  @BeforeTest
  public void setup()
  {
      System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);    
  }
}

答案 1 :(得分:0)

这些可能是一些问题:

  1. “我的路径”不是有效的变量名称。将其更改为“my_path”。

  2. String my_path = "C:\\Utility\\BrowserDrivers\\chromedriver.exe";定义为全局变量。

  3. 提供System.setProperty("webdriver.chrome.driver","my_path");

  4. 时检查变量
  5. 请勿使用driver.manage().window().maximize();处理选项类。

  6. WebDriver driver是在类级别全局定义的,您不必将其作为参数传递给IO_login()

  7. 检查此代码:

    public class TestAnyURL_TestNG
    {
    WebDriver driver;
    String url = "http://google.com";
    String my_path = "C:\\Utility\\BrowserDrivers\\chromedriver.exe";
    
    @Test (priority = 0)
    public void IO_login() 
    {
    
        //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() 
    {
        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);        
    }
    }
    
  8. 请记住:

    1. http://google.com替换为own_test_URL

    2. package demo替换为package your_package_name

    3. public class TestAnyURL_TestNG替换为public class your_class_name

    4. 以“TestNG Test”

    5. 运行
    6. 更新状态。

答案 2 :(得分:0)

请用下面的代码替换,然后尝试,我在我的机器上测试过。

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;

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

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

    //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*");

    driver = new ChromeDriver();

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

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

以下是我对原始代码所做的更改。

1.删除驱动程序参数到IO_login方法()

2.再次在setup方法中删除了web驱动程序的声明,我们已经在类中和所有方法之外声明了它。

如果您有任何疑问,请与我们联系。

答案 3 :(得分:0)

问题出在TestNG的设置上。以下代码解决了我的问题:

@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);        
}