Jaw. M.
Jaw. M.

Reputation: 149

Build failed with an exception. java.lang.AssertionError while passing parameter to Gradle

I have a question about passing a parameter to the gradle build script. First of all I have a selenium test class:

public class TestHH extends HHTest{

    @Parameters({ "platform", "browser", "version"})
    @BeforeTest(alwaysRun = true)
    public void setup(String platform, String browser, String url, String version) throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setPlatform(org.openqa.selenium.Platform.WIN10);
        System.setProperty("java.net.preferIPv4stack", "true");
        caps.setCapability("SeleniumTests", "redhat5 && amd64");

        assertEquals(System.getProperty(url), url);

        if (browser.equalsIgnoreCase("firefox")) {
            System.out.println("Executing on Firefox");
            String Hub = "http://localhost:4444/wd/hub";

            caps = DesiredCapabilities.firefox();
            caps.setBrowserName("firefox");

            System.setProperty("webdriver.gecko.driver", "/opt/geckodriver.exe");

            driver = new RemoteWebDriver(new URL(Hub), caps);

            driver.navigate().to(url);
            driver.manage().window().maximize();

        } else if (browser.equalsIgnoreCase("chrome")) {
            System.out.println("Executing on Chrome");
            String Hub = "http://localhost:4444/wd/hub";

            caps = DesiredCapabilities.chrome();
            caps.setBrowserName("chrome");
            ChromeOptions options = new ChromeOptions();
            System.setProperty("webdriver.chrome.driver", "/opt/chromedriver.exe");
            caps.setCapability(ChromeOptions.CAPABILITY, options);

            options.addArguments("--start-maximized");
            driver = new RemoteWebDriver(new URL(Hub), caps);
            driver.navigate().to(url);
        } else {
            throw new IllegalArgumentException("The Browser Type is undefined");
        }
    }

This is my build.gradle script:

apply plugin: 'java'
apply plugin: 'eclipse'


jar {
    version  '1.0'
    baseName 'SeleniumStarter'
    extension '.jar'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8


description = ""

repositories {

    mavenCentral()
    mavenLocal() 
}

ext.seleniumVersion = '3.7.1'

dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-edge-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-firefox-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-api', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:seleniumVersion

    compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'
    testCompile group: 'junit', name: 'junit', version:'4.12'
    testCompile group: 'org.testng', name: 'testng', version:'6.11'
}

test {

    systemProperties(System.getProperties())
     println 'test'
     println System.properties['url'] // print for testing purposes 

     systemProperty 'url', System.getProperty('url')
     useTestNG() {
       suites 'src/test/resources/TestHH.xml'

     }

 }

eclipse {
  classpath {
  containers 'org.springsource.ide.eclipse.gradle.classpathcontainer'}
}

// A custom task to show report on tests that have run
task viewResults(dependsOn: ['test'] , type:Exec) {
        workingDir './build/reports/tests'      
        commandLine 'cmd', '/c', 'start index.html' 
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.10' //we want gradle 2.10 to run this project
}

task logInfo (dependsOn: test){
    logging.captureStandardOutput LogLevel.INFO
    doLast {

        println 'test'
        println System.properties['url']
        println 'url'



    }

}

The parameter which I'm trying to pass by using gradle command is an url. I'm passing other parameters like platform, browser, version by using the testng xml file.

I start the following command to pass the required parameter but it doesn't work.

  gradle test -Durl="http://live-test1.hamburg.de"

It launches Firefox and Chrome without any url.

I'm getting this output with an exception after I've started the above command :

FAILURE: Build failed with an exception.
java.lang.AssertionError

Upvotes: 1

Views: 2848

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193338

The error you are seeing says it all :

java.lang.AssertionError

You have tried to invoke System.getProperty(url) in the line :

assertEquals(System.getProperty(url), url);

If you look at the Java Docs of System Properties the Properties object supports the following system properties :

  • "file.separator"
  • "java.class.path"
  • "java.home"
  • "java.vendor"
  • "java.vendor.url"
  • "java.version"
  • "line.separator"
  • "os.arch"
  • "os.name"
  • "os.version"
  • "path.separator"
  • "user.dir"
  • "user.home"
  • ""user.name""

So clearly, System.getProperty(url) is not a valid expression which returns unexpected results (possibly a NULL or a NullPointerException) which is not a valid type of argument for assertEquals. Hence, assertEquals fails and you see a java.lang.AssertionError.

Upvotes: 2

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

The problem lies in your code.

The crux of the issue lies in the statement in your setup() method.

You have

assertEquals(System.getProperty(url), url);

The problem with this line is that System.getProperty() accepts a String constant as a key for which it would retrieve a value.

So ideally speaking since you are passing in the JVM value from the command line via

gradle test -Durl="http://live-test1.hamburg.de"

Notice here your JVM argument's name is "url"

Your java code should be reading the value of this JVM argument via

System.getProperty("url")

and not via

System.getProperty(url)

The former statement instructs Java to query for the value of "url" from the list of JVM arguments passed.

The latter statement instructs Java to query from the list of JVM arguments for a key, which could be anything [ Since url is a variable ].

So please change

assertEquals(System.getProperty(url), url);

to

assertEquals(System.getProperty("url"), url);

I also noticed that you have a mismatch in the number of parameters in your setup() method.

It currently reads as

@Parameters({ "platform", "browser", "version"})
@BeforeTest(alwaysRun = true)
public void setup(String platform, String browser, String url, String version) throws MalformedURLException {
...
}

So you are instructing TestNG to inject 3 parameter values from what it finds as <parameters> values from the suite xml file, but your method actually contains 4 parameters. So TestNG wouldn't know what to inject for the variable String url and thus throw an exception which would look like below

org.testng.TestNGException: 
Parameter 'url' is required by BeforeTest on method beforeTest but has not been marked @Optional or defined

Upvotes: 1

Related Questions