Steps:
1. In eclipse I've created a new project named 'ForSe'.
Login.java
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
src
ForSe_TestCases.java
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
[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
===============================================
These can be a few of the issues:
"my path" is not a valid variable name. Change it to "my_path".
Define a String my_path = "C:\\Utility\\BrowserDrivers\\chromedriver.exe";
as a global variable.
Check the variable when you provide System.setProperty("webdriver.chrome.driver","my_path");
Don't use driver.manage().window().maximize();
instead handle it with Options Class.
WebDriver driver
is defined globally at Class level, you don't have to pass it as an argument in IO_login()
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:
Replace http://google.com
with your own_test_URL
.
Replace package demo
by package your_package_name
.
Replace public class TestAnyURL_TestNG
with public class your_class_name
Run as "TestNG Test"
Update me the status.