3

I am using selenium webdriver, maven for dependency and java lanaguage and I'm following page object design pattern. This is my first time I am using maven. I am automating a login page. Here browser should be open but the username and password can't be automate.. Here is the code..

package pageobjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
 * Created by naveen on 29/11/16.
 */
public class HomePage {
    @FindBy(how= How.NAME, using = "username")
    WebElement username;
    @FindBy(how=How.NAME, using = "password")
    WebElement password;
    @FindBy(how=How.XPATH, using="//*[@id=\'login-container\']/form/div[3]/div/p/input[1]" )
    WebElement button;

    //enter  username
    public void  setUsername(String user)
    {
        username.sendKeys(user);
    }
    //enter password
    public void setPassword(String pass)
    {
        password.sendKeys(pass);
    }
    //click login
    public void clickButton()
    {
        button.submit();
    }


}

Steps:

package steps;


import org.openqa.selenium.support.PageFactory;
import pageobjects.HomePage;

import util.DriverManager;

/**
 * Created by srikanth on 20/11/16.
 */
public class LoginSteps {

    public HomePage Login(String name, String pwd) {
        HomePage homePageObj = PageFactory.initElements(DriverManager.driver, HomePage.class);
        homePageObj.setUsername(name);
        homePageObj.setPassword(pwd);

        homePageObj.clickButton();

        return homePageObj;
    }
}

Login:

package login;


import org.testng.annotations.*;

import org.testng.Assert;
import pageobjects.HomePage;
import steps.LoginSteps;
import util.DriverManager;

public class loginTest
{
    @BeforeSuite(groups = {"regression"})
    public void initDriver(){
        DriverManager manager = new DriverManager();
    }


@Test 
@Parameters({"userName","password"})
    public void verifyValidLogintoSpree(String name, String pwd)
    {
        LoginSteps loginSteps = new LoginSteps();
       HomePage expected_message=loginSteps.Login(name,pwd);
        Assert.assertEquals(expected_message,"MY ACCOUNT");

    }
}

TestNG config:

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Regression">
      <parameter name="userName" value="geosony"/>
      <parameter name="password" value="1"/>
      <test name="regression">
         <groups>
           <run>
            <include name="regression"/>
            <exclude name="sanity"/>
           </run>
         </groups>
         <classes>
           <class name="login.loginTest"/>
        </classes>
      </test>
   </suite>

Error :

org.testng.TestNGException: Parameter 'userName' is required by @Test on method verifyValidLogintoSpree but has not been marked @Optional or defined in /home/naveen/.IdeaIC2016.2/system/temp-testng-customsuite.xml

at org.testng.internal.Parameters.createParameters(Parameters.java:156) at org.testng.internal.Parameters.createParameters(Parameters.java:370) at org.testng.internal.Parameters.handleParameters(Parameters.java:495) at org.testng.internal.Invoker.handleParameters(Invoker.java:1253) at org.testng.internal.Invoker.createParameters(Invoker.java:980) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1070) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:753) at org.testng.TestRunner.run(TestRunner.java:607) at org.testng.SuiteRunner.runTest(SuiteRunner.java:368) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321) at org.testng.SuiteRunner.run(SuiteRunner.java:270) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284) at org.testng.TestNG.runSuitesLocally(TestNG.java:1209) at org.testng.TestNG.runSuites(TestNG.java:1124) at org.testng.TestNG.run(TestNG.java:1096) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Default Suite Total tests run: 1, Failures: 1, Skips: 0 Process finished with exit code 0

18
  • Which error it will shows? You can add your eclipse error msg here. Nov 30, 2016 at 5:38
  • @Bharat Mane Hi.. Do you get this error ? please help me I'm in stuck.
    – user21268
    Nov 30, 2016 at 6:15
  • Please check by adding @Test annotation to "verifyValidLogintoSpree" Method.
    – QAMember
    Nov 30, 2016 at 6:46
  • @QAMember yes I add the Test annotation. but the same error came. I want to automate enter username and password. When i run logintest. it only open the browser.
    – user21268
    Nov 30, 2016 at 7:01
  • Test method is mising @Test annotation, in addition it looks like in testng.xml parameters tag should not be included into test tag testng.org/doc/documentation-main.html#parameters
    – olyv
    Nov 30, 2016 at 7:09

4 Answers 4

4

You may try with following changes:

package pageobjects;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class HomePage {
    @FindBy(name="username")
    WebElement u_Name;

    @FindBy(name="password")
    WebElement passwd;

    @FindBy(xpath="//*[@id=\'login-container\']/form/div[3]/div/p/input[1]" )
    WebElement button;

    //User Login
    public void  userLogin(String user, String pass)
    {
        u_Name.sendKeys(user);
        passwd.sendKeys(pass);
        button.click();
    }

Use your rest of the code and execute....

5
  • sorry not working. I tried but same error repeating. It open the browser. But doesn't automate the enter username and password and click submit..
    – user21268
    Nov 30, 2016 at 7:09
  • @Test annotation is missing, it does not finds an tests to execute after launching browser. Check your code once again... Nov 30, 2016 at 9:32
  • Ya i did but still same error occurs
    – user21268
    Nov 30, 2016 at 10:09
  • send me the url... Nov 30, 2016 at 10:13
  • sorry I can't its our company project.
    – user21268
    Nov 30, 2016 at 10:16
4

When you select a single test and run it TestNG creates a custom testng file (temp-testng-customsuite.xml in your case, see the exception). If you want to pass parameters to test from testng you should either run it as suite or you have to specify your xml as standard xml template (if you are using eclipse). If you are running the test using maven, add the following codes in pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <suiteXmlFiles>
        <!-- TestNG suite XML files -->
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>
2
  • Yes I already add the dependencies selenium and testng
    – user21268
    Nov 30, 2016 at 11:04
  • The above code is not to add jar dependencies, but to execute your testng.xml using maven Nov 30, 2016 at 11:08
0

You have the wrong location and a syntax error with the parameters in the testng.xml, replace the ></parameter> with />:

You have: <parameter name="userName" value="[email protected]"></parameter>

Should be: <parameter name="userName" value="[email protected]"/>

Also in the documentation it is placed outside of the <test> and inside the <suite>.

Atleast according to the TestNG documentation about parameters:

<suite name="My suite">
  <parameter name="first-name"  value="Cedric"/>
  <test name="Simple example">
  <-- ... -->

Not sure about this, but if you run your test directly in the IDE it might not use the testng.xml. Can you try and run from the commandline with maven? Maybe also look at: https://stackoverflow.com/questions/32658391/parameter-name-is-required-by-test-on-method-parametertest-but-has-not-been-m

9
  • @Niles You have: <parameter name="userName" value="[email protected]"></parameter> Should be: <parameter name="userName" value="[email protected]"/> I think both the lines are same. What is the difference? Nov 30, 2016 at 8:51
  • @Niels van Rejimersdal I corrected all mistakes but same error came..
    – user21268
    Nov 30, 2016 at 8:52
  • @BharatMane No idea, I am no XML expert. Nov 30, 2016 at 9:02
  • @AntonyNaveen The error says the function/method verifyValidLogintoSpree is not getting its parameters. You need to look into why the TestNG parameters are not being loaded. My first guess was the syntax as you do not match the documentation, now I don't know. :) Nov 30, 2016 at 9:05
  • 1
    @Niels : As you suggested above answer both lines have the same meaning in XML. We can write Syntax in XML in any way either starting and ending tag of the same parameter. It means he also written the same. Now we have to check where this error comes from? Nov 30, 2016 at 9:14
0

Same error was coming for me. But after running with TestNG suite instead of TestNG Class, it worked. Follow the steps below to run your class as TestNG suite -

  1. Right click on the class --> TestNG --> Convert to TestNG

Click on the link to see the picture - https://i.stack.imgur.com/eZW4t.jpg

  1. testng.xml pop up will appear. Click on Finish.

Click on the link to see the picture - https://i.stack.imgur.com/cIG6L.jpg

  1. testng.xml file will be created. See the below screenshot.

Click on the link to see the picture - https://i.stack.imgur.com/MNu2O.jpg

  1. Open testng.xml file and provide the required parameters.

Click on the link to see the picture - https://i.stack.imgur.com/2D6er.jpg

  1. Provide the same parameter inside your class. See the below screenshot.

Click on the link to see the picture - https://i.stack.imgur.com/7ly2Z.jpg

  1. Right click on generated testng.xml file --> Run As --> TestNG Suite

Click on the link to see the picture - https://i.stack.imgur.com/BIjW6.jpg

  1. Test case will pass and below output will be printed (As of my code)

Click on the link to see the picture - https://i.stack.imgur.com/81Tvk.png

Hope this will help. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.