I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the
webdriver.chrome.driver system property; for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know,
Which PATH should I place that? or
How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
#Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
#Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "your\path\to\it");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Related
`
public class CucumberHooks {
public static void main(String[] args) {
}
protected static WebDriver driver;
#Before
public void setup() throws IOException {
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("extension_5_3_2_0.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
String browser = ReadPropertyFileSingleton.getInstance().getProp("browser");
driver = Util.setEnvironmentAndGetDriver(browser);
assert driver != null;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static WebDriver getDriver() {
return driver;
}
#After
public void close() {
driver.quit();
}}
The code where I added adblock extensions via crx file
When I run tests through a feature file, a regular browser without a blocker is launched
the browser is also launched already with a blocker, but it does not see the steps
what could be the problem and is it possible to use adblock this way?
I have planned to customize the testNG reports. So I have used ExtentReports with the below codes.
Selenium runs properly without any issues but the report is not generated in the specified folder location.
I have added ExtentReport 2.41.2 maven dependency in my pom.xml file.
Sample Code:
public class ExtentA
{
public static ExtentReports extent;
public static ExtentTest logger;
public static WebDriver driver;
#BeforeSuite
public void config()
{
extent = new ExtentReports("E:/Automation/MyReport.html", true);
extent.loadConfig(new File("E:/Automation/extentreports-java-2.41.2/extentreports-java-2.41.2/extent-config.xml"));
}
#BeforeMethod
public void beforeMtd(Method method)
{
logger = extent.startTest("sample", "test case desc");
logger.assignAuthor("Mohan");
}
#Test
public void sample()
{
System.setProperty("webdriver.chrome.driver", "C:/selenium/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.co.in");
logger.log(LogStatus.PASS, "Browser Launched successfully");
System.out.println("Browser launched");
driver.manage().window().maximize();
}
#AfterMethod
public void close()
{
driver.close();
driver.quit();
extent.endTest(logger);
}
}
Before running the test case, I have created the html file in loca, but the report is not generated.
Add extent.flush(); to the end of your close method.
I have a parent web test which is called by the test class like this:
public class Webtest
{
protected static WebDriver driver;
#BeforeMethod
public static WebDriver openUrl(String URL)
{
driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Driver started :"+ driver);
return driver;
}
public static WebDriver closeDriver(){
driver.quit();
System.out.println("Driver closed :"+ driver);
}
}
Now I implement the above in a test class extending it.
public class testClass extends WebTest
{
#Test
public void TC01()
{ System.out.println("Test1:" + driver);
// Do something using driver
}
#Test
public void TC02()
{
System.out.println("Test2:" + driver);
// Do something
}
}
Now the results:
Driver started :FirefoxDriver: firefox on MAC (dfe4e055-4555-0d4d-8a83-a9a802159ea7)
Test1:FirefoxDriver: firefox on MAC (dfe4e055-4555-0d4d-8a83-a9a802159ea7)
Driver closed :FirefoxDriver: firefox on MAC (dfe4e055-4555-0d4d-8a83-a9a802159ea7)
Driver started :FirefoxDriver: firefox on MAC (1370df47-483b-574c-9792-9bb5fa077364)
Test2:FirefoxDriver: firefox on MAC (1370df47-483b-574c-9792-9bb5fa077364)
[Error] resulted in an exception: The FirefoxDriver cannot be used after quit() was called.
Basically, I assume the test2 is using the previous driver. I haven't called the second driver to quit yet but am getting the error. I did put sleep in between every method and tried to but nothing worked. Any help? I am using firefox 46.
Since you defined your openUrl method as a #BeforeMethod it is being called before all other methods, so when your testClass calls TC01, it opens a new browser first. Then when you call TC02, the #BeforeMethod gets invoked again and another, new WebDriver is created. That does not explain the last line error, though. I'm not sure why it would ever execute the quit method since the closeDriver method never seems to be called.
Here is the solution to your Question:
You need to address a lot of issues in your code as follows:
In Webtest class, within closeDriver() method, you are quit the driver driver.quit() still you trying to get WebDriver in return public static WebDriver closeDriver().
So change it to public static void closeDriver()
In testClass class, you are trying to extend WebTest but your base class is Webtest.
So change it to public class testClass extends Webtest
To work with Selenium 3.4.0 along with geckodriver v 0.16.1 & latest Mozilla Firefox 53.x you need to download the latest gecko driver and mention the absolute path in your code theough System.setProperty before you initialise the driver as follows:
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
driver = new FirefoxDriver();
In Webtest I can see your are accepting the URL as an argument public static WebDriver openUrl(String URL) but I dont see you passing it from Webtest. Keep it simple, define the String URL within openUrl() method as follows:
String URL = "http://gmail.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get(URL);
In closeDriver() you are quitting the driver by driver.quit(); next if you System.out.println("Driver closed :"+ driver); you won't get any realtime value of the driver instance but only null.
Finally, the entire chaos is created because you have added #BeforeMethod annotation to open the driver but haven't released it closeDriver() method using #AfterMethod annotation. Add the annotation #AfterMethod.
Here is your own working code along with some simple tweaks:
class Webtest :
public class Webtest
{
protected static WebDriver driver;
#BeforeMethod
public static WebDriver openUrl()
{
String URL = "http://gmail.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Within openUrl() - Driver started :"+ driver);
return driver;
}
#AfterMethod
public static void closeDriver()
{
driver.quit();
System.out.println("Driver closed :"+ driver);
}
}
class testClass :
public class testClass extends Webtest
{
#Test
public void TC01()
{
System.out.println("Within TC01 - Test1:" + driver);
// Do something using driver
}
#Test
public void TC02()
{
System.out.println("Within TC02 - Test2:" + driver);
// Do something
}
}
Let me know if this Answers your Question.
I have downloaded all the JUnit jar files and installed them.When I run my package as JUnit,it says no tests found with TestRunner 'JUnit 4'.What am I missing
*public class AmazonTestFactory{
static WebDriver driver;
#Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
driver.get("http://www.amazon.co.uk");
Thread.sleep(5000);
driver.manage().window().maximize();
}
#Test
public void testHomePage(){
AmazonHomePageFactory Page = PageFactory.initElements(driver, PageFactoryModelExercise.AmazonHomePageFactory.class);
Page.enterProductInSearchBox();
Page.clickSearchButton();
}
#After
public void closeBrowser(){
driver.close();
}*
I have a method called test, where I have defined a Webdriver object like this:
FirefoxProfile firefoxprofile = new FirefoxProfile();
firefoxprofile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(firefoxprofile);
Selenium selenium = new WebDriverBackedSelenium(driver, "https://10.17.17.212:4343");
i can use selenium commands in this function, like
selenium.click() etc.
but I cannot use them in function that I call from this function.
e.g. i call a function called set() (private int set)
inside set
i resolved it by declaring WebDriver variable as static.
in my code it looks like:
public class BaseSeleniumTest extends SeleneseTestBase {
static WebDriver driver;
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
driver = new FirefoxDriver();
}
#Before
public void homePageRefresh() throws IOException {
driver.get(propertyKeysLoader("login.base.url"));
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
....
//blablabla}
Make selenium object outside method and declare it as static so that you could use it globally.