Sanchit Sanchit - 3 years ago 349
Java Question

Selenium script fails with TestNG annotation

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


  1. Under 'src' folder → 'default package' I created a class named
    Login.java
    .

  2. 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();
    }
    }



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.


Steps taken to create TestNG script is as follows:


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



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
===============================================

Answer Source

These can be a few of the issues:

  1. "my path" is not a valid variable name. Change it to "my_path".

  2. Define a String my_path = "C:\\Utility\\BrowserDrivers\\chromedriver.exe"; as a global variable.

  3. Check the variable when you provide System.setProperty("webdriver.chrome.driver","my_path");

  4. Don't use driver.manage().window().maximize(); instead handle it with Options Class.

  5. WebDriver driver is defined globally at Class level, you don't have to pass it as an argument in IO_login()

  6. Check this code:

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

    }

Remember to:

  1. Replace http://google.com with your own_test_URL.

  2. Replace package demo by package your_package_name.

  3. Replace public class TestAnyURL_TestNG with public class your_class_name

  4. Run as "TestNG Test"

  5. Update me the status.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download