0

1.TestCommands.java

public class TestCommands {
public void click(By locator,WebDriver driver){
    try {
        WebElement element = driver.findElement(locator);
        element.click();            
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }       
}
public void type(By locator,WebDriver driver,String key){
    try {
        WebElement element = driver.findElement(locator);
        element.clear();
        element.sendKeys(key);          
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }       
}
public void open(String url,WebDriver driver) {
    try {
        driver.get(url);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }       
}

}

2.LoginPage.java

public class LoginPage extends TestCommands{
//web elements
public By userName(){
    return By.xpath("//input[@id='user-name']");
}
public By password(){
    return By.xpath("//input[@id='user-password']");
}
public By loginButton(){
    return By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='SIGN IN'])[1]/following::div[2]");
}

//Business functions
public void openApplication(String url,WebDriver driver){
    open(url,driver);
}
public void loginToApplication(WebDriver driver,String userName,String password){
    type(userName(),driver,userName);
    type(password(),driver,password);
    click(loginButton(),driver);
}

}

3.TC_LoginToTest.java

public class TC_LoginToTest {
WebDriver driver ;
String baseUrl;
LoginPage login = new LoginPage(); 

@Parameters("browser")
@BeforeMethod
public void beforeMethod(String browser) {

    if (browser.equals("firefox")) {
        System.setProperty("webdriver.gecko.driver", "drivers\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    } else if (browser.equals("chrome")) {
        System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
        driver = new ChromeDriver();            
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }
}  
@DataProvider
public Object[][] tc001() {
    return new Object[][] {
            new Object[] {"nadee","12345678"},      
    };
}

@Test(dataProvider="tc001")
public void tc001(String userName , String password) {
    driver.get(baseUrl);
    login.openApplication("https://test.com", driver);
    login.loginToApplication(driver, userName, password);
}
}

I have use the xml file as follows to send the browser type,

**4.xml file**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
  <parameter name="browser" value="chrome" />
    <classes>
      <class name="Systemloginfunctionalities.TC_LoginToTest"/>
    </classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

But I have received an error as follows in the TestNG , java.lang.NullPointerException: null value in entry: url=null

Please help me to find a solution for this.

0

Here

@Test(dataProvider="tc001")
public void tc001(String userName , String password) {
    driver.get(baseUrl);
    login.openApplication("https://test.com", driver);
    login.loginToApplication(driver, userName, password);
}

you have driver.get(baseUrl); that receives baseUrl which is null (you are not assigning a value for that string anywhere)

  • Thanks for your response.my test successfully passed,but i just wanted know whether I can print a success message for the try catch codes in the TestCommands.java to the console as well,can i have some advice from you?as i am new to the automation testing – user3806999 Oct 9 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.