where to keep system.property in TestNG test case

3k views Asked by At

Where should I add the following statement System.setProperty("webdriver.gecko.driver","//home//tuser//software//selenium//gecko_driver//geckodriver");

so that my code does not produce an error

public class class1 {

public static String baseURL = "https://facebook.com/";
public WebDriver driver = new FirefoxDriver();

@Test(priority = 1)
public void login() throws InterruptedException {

    driver.get(baseURL);
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys("srikanth");
    driver.findElement(By.id("password")).sendKeys("sri");
    driver.findElement(By.id("login_button")).click();
    Thread.sleep(30000);
}
3

There are 3 answers

0
Anand On BEST ANSWER

I have used the answer provided by Jainish, but it didn't worked for me. For the same, I found out the following resolution:

    public class class1 {

    //public WebDriver object so that it can be used anywhere within class1
    public WebDriver driver; 

    //public WebDriverWait object so that it can be used anywhere within class1
    public WebDriverWait wait;

    @BeforeClass
    public void setup()
        {
            System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
            driver = new FirefoxDriver();
            wait = new WebDriverWait(driver,15); //explicit wait to increase performance
        }

    @Test()
    public void login() {

        driver.get("https://facebook.com/");
        driver.manage().window().maximize();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        driver.findElement(By.id("username")).sendKeys("srikanth");
        driver.findElement(By.id("password")).sendKeys("sri");
        driver.findElement(By.id("login_button")).click();

        }
    }

I have modified the code written by Jainish. In the modified code, you can see that objects are declared with public scope outside any method, hence, allowing us to use this object anywhere within class1.

Also, there is no need to use static wait (which is Thread.sleep) and at implicit wait at the same time. Instead use Explicit wait to increase the script performance.

Hope, the above is useful. Please let me know if I am wrong anywhere.

7
prithvi394 On

See the snippet below

public class class1 {
@Test(priority = 1)
public void login() throws InterruptedException {
    String baseURL = "https://facebook.com/";
    System.setProperty("webdriver.gecko.driver","C:\\Selenium Training\\Practice\\Firefox driver\\geckodriver.exe"); //Make sure to provide .exe extn
    WebDriver driver = new FirefoxDriver();
    driver.get(baseURL);
    driver.manage().window().maximize();
    driver.findElement(By.id("email")).sendKeys("srikanth");
    driver.findElement(By.id("pass")).sendKeys("sri");
    driver.findElement(By.id("u_0_n")).click();
    Thread.sleep(30000);
    }
  }
2
Jainish Kapadia On
public class class1 {

@Test
public void setup() throws InterruptedException
    {
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);        
    }

@Test(dependsOnMethods = {"setup"})
public void login() throws InterruptedException {

    driver.get("https://facebook.com/");
    Thread.sleep(2000);
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys("srikanth");
    driver.findElement(By.id("password")).sendKeys("sri");
    driver.findElement(By.id("login_button")).click();
    Thread.sleep(5000);
    }
}