TestNG with IE webdriver

1.2k Views Asked by At

Hi Im a newbie to selenium, i was trying to use TestNG with IE webdriver, Now i cant instantiate the IE driver directly under the class (Not the main method). When i do that i get the below error: Multiple markers at this line - Syntax error on tokens, FormalParameter expected instead - Syntax error on token(s), misplaced construct(s) - Syntax error on token ""webdriver.ie.driver"", invalid

If i then put on a method with @BeforeSuite annotation, i need to pass the driver to every other test method in the class. Is there a way where i can by pass this passing the driver object.

Find below the sample code i am using:

package FirstTestNGPackage;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FirstTestNGclass {
    
    @BeforeSuite 
    public void SetDriverPaths()
    {
        File IEDriver = new File("C:\\Users\\REDACTED\\Desktop\\SeleniumJars\\IE Driver\\IEDriverServerX64_2.44.0.exe");
        System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
        WebDriver Driver = new InternetExplorerDriver();
    }
    
   
  @Test
  public void tester()
  {
      Driver.findElement(By.id("keywordInput")).sendKeys("REDACTED");
      Driver.findElement(By.id("keywordInput")).sendKeys(Keys.ENTER);
      Assert.assertEquals(Driver.findElement(By.xpath(".//*[@id='search_results']/h1/span/strong")).getText(), "REDACTED");
  }
  
  @BeforeTest
  public void RMSLogin() 
  {
      Driver.navigate().to("REDACTED");  
          
  }
  
  @AfterTest
  public void closeBrowser()
  {
    
      Driver.quit();
      
  }
  
}

2

There are 2 best solutions below

0
On BEST ANSWER

init you webdriver like this then you can use it in every method with this.driver.

public class FirstTestNGclass {

    public WebDriver driver;

    @BeforeSuite 
    public void SetDriverPaths()
    {
        // ....
        this.driver = new InternetExplorerDriver();
    }

    // ....

}
0
On

One of the simple solution to this problem is to use a common driver class for your Test suite...so that we can use the same driver instance in all classes

Common driver class

public class Driver {

    public static WebDriver driver=null;



    public static WebDriver startdriver(String browser){


        if(browser.equalsIgnoreCase("Chrome")){

        System.setProperty("webdriver.chrome.driver", "path");

        driver=new ChromeDriver();

        }else if(browser.equals("IE")){

        System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());

        driver=new InternetExplorerDriver();

        }
        return driver;

        }

    }

you can create a driver instance like this

Driver.startdriver("IE");

You can use the driver object like classname.instance

Driver.driver.findElement(By.xpath("path"));

Hope this helps you...if you have any queries Kindly get back