-2

I am new to selenium. Trying to use testNG framework. But receiving the below error. I am trying a simple code. Please help me out.I am trying to run the below code:

package testng_project;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Sample {


// declaration and instantiation of objects/variables
String key= "webdriver.gecko.driver";
String value= "C:\\geckodriver-v0.10.0-win64\\geckodriver.exe";
System.setProperty(String key,String value);
//public static String setProperty(String key,String value);
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";


@Test
public void verifyhomepage_title()
{
    driver.get(baseUrl);
    String expectedtitle = "Welcome: Mercury Tours";
    String actualtitle=driver.getTitle();
    Assert.assertEquals(actualtitle,expectedtitle);
    driver.quit();
}

}

While running the code I am getting compilation error.Please find the below error:

Caused by: java.lang.Error: Unresolved compilation problems: Syntax error on token "setProperty", Identifier expected after this token Return type for the method is missing This method requires a body instead of a semicolon

at testng_project.Sample.<init>(Sample.`enter code here`java:15)
... 27 more

Let me know how to resolve the error.

Thanks.

Soumyajit Das
  • 11
  • 1
  • 1
  • 1

4 Answers4

3

Put System.setProperty() in :

public static void main(String[] args)

Then it will work!

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
2

I see two issues with your call to System.setProperty():

  1. It needs to go into a method (it cannot stand among the field declarations as it does in your code).
  2. It needs to follow the syntax for a method call.

For item 1., I don’t know TestNG, so I’m unsure, but I believe you can do:

@BeforeTest
public void setup() {
    System.setProperty(key, value);
}

Somebody please correct me if I’m not entirely correct here.

For item 2. the declaration of a method and a call to it are similar in some ways (on purpose) and different in some (because a declaration and a call are two different things). So if you read the declaration of System.setProperty(), for instance in the documentation, you are correct that it says setProperty(String key, String value). In the declaration, you need to give the type of the parameters.

In the call, Java already know the types of the arguments you provide, so it doesn’t allow you to repeat those. Look at your call to driver.get(), which is correct. You have already declared baseUrl a String, so you don’t repeat that in the call. Similarly, don’t repeat the word String in your call to System.setProperty(). Just do:

System.setProperty(key, value);

This should work.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Just get the System.setProperty into a method. All it is asking that it needs to be in a Parenthesis, a Method! May it be a @BeforeSuite method you use to initiate your test!

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Kalyan .P
  • 3
  • 4
0

Hope following should work.

public class Sample {

//declaration and instantiation of objects/variables

    String key = "webdriver.gecko.driver";
    String value = "C:\\geckodriver-v0.10.0-win64\\geckodriver.exe";

    WebDriver driver = new FirefoxDriver();
    String baseUrl = "http://newtours.demoaut.com";

@BeforeSuite

    public void setUp() {   
        
    System.setProperty(key,value);  
    driver = new FirefoxDriver();
    }

@Test

    public void verifyhomepage_title(){

    driver.get(baseUrl);
    String expectedtitle = "Welcome: Mercury Tours";
    String actualtitle = driver.getTitle();
    Assert.assertEquals(actualtitle,expectedtitle);
    driver.quit();
}

}
Hoppo
  • 1,130
  • 1
  • 13
  • 32