How Caused by: java.lang.ClassNotFoundException:com.google.common.base.function will be handled in selenium? - selenium

Caused by: java.lang.ClassNotFoundException:com.google.common.base.function will be handled in selenium
got this error when running through Webdriver.
public class Logintoaccount {
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.ie.driver", "d:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
}
#Test
public void test() {
//System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
}
}

I think you are missing the Selenium standalone server in your build path. Add the required version and you would be good to go...
selenium-server-standalone-version.jar

Add selenium-server-standalone-version.jar file in the project. the issue will be resolved

Use #BeforeTest instead of #Before, since this setup() is going to run at the beginning of the test.

Here is the solution for the problem you are facing:
You have used the Annotation as #Before. Going by the documentation of TestNG you can only use the following Annotations starting with #Before :
#BeforeSuite
#BeforeClass
#BeforeTest
#BeforeGroups
#BeforeMethod
Use the one which suits your test strategy. This will solve your issue. Do remember to import the classes​ from TestNG only.
Let me know if this helps you.

Related

How to set WebDrivers(chrome, Firefox,etc.) path in build.gradle rather than setting it in code

I would like to set the WebDrivers path in build.gradle rather than setting the absolute path of the driver. This would help me from not checking-in the .exe into the repo.
I am setting the driver as
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); in code.
Instead of the specifying the "/path/to/chromedriver" in code, I would like it to be picked/set by gradle whenever the test runs.
Tried below in build.gradle
test {systemProperty "webdriver.chrome.driver",
classpath.find {
it.name.contains("selenium-chrome-driver")
}
println "Getting system properties"
println System.getProperty("webdriver.chrome.driver")
}
But each I run the test, I get null
Thanks in advance.
You could also use WebDriverManager( https://github.com/bonigarcia/webdrivermanager ) to manage your drivers.
https://github.com/bonigarcia/webdrivermanager#basic-usage shows the simple way to add via gradle
dependencies {
testImplementation("io.github.bonigarcia:webdrivermanager:4.3.1")
}
and code usage:
#BeforeClass
public static void setupClass() {
WebDriverManager.chromedriver().setup();
}
#Before
public void setupTest() {
driver = new ChromeDriver();
}

Is the #After annotation always executed in Selenium when an exception occurs?

In the case of an exception occuring; is it necessary to close the driver in a catch block when writing a Selenium test case?
I closed the driver in the tearDown() method which has the #After annotation.
No, you don't need to close/quit the driver in catch block if you have done that in #After annotation.
Here.
All #After methods are guaranteed to run even if a Before or Test
method throws an exception.
taken from following website
http://junit.sourceforge.net/javadoc/org/junit/After.html
as it doesn't matter exception has occurred or not this annotation is going to be executed.
Following example is using TestNG but I think #After annotation of JUnit is same as #AfterMethod of TestNG
In below example, even though test will throw java.lang.ArithmeticException: / by zero the tearDown method will be executed and will close all the browsers.
public class GoogleTest {
WebDriver driver;
#BeforeMethod
public void setup() {
driver = new FirefoxDriver();
}
#AfterMethod
public void tearDown() {
driver.quit();
System.out.println("Closed all the browsers");
}
#Test
public void visitGoogle(){
driver.get("https://google.com");
int a=1/0;
}
}
In the Junit report you will see two thing.
Error: if any exception has occurred it will be reported as error
Failure: if any assertions has failed then it will be reported as Failure
I wouldn't handle the exception until you want to continue the flow. Exception like ElementNotFound or ElementNotVisible of webdriver is better unhandled. It will fail the test and you can see the whole stack trace, which will help you debug the root cause.
#After is nothing to do with selenium. It is an annotation of JUNIT if you are using java. There is no such rule to close driver in tearDown() or specify #After function. While it is good practice to quit driver instance once the testcase done. quit will close all your browser instances and clear memory and resources in machine if another script going to trigger.
Moreover, handling the error is also a good practice. You should handle the errors which can occur during script execution in runtime. example :- I/O operation, file upload etc
Example :-
WebDriver driver=null;
#BeforeMethod
public void setup() {
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
}
#AfterMethod
public void tearDown() {
//driver.quit();
System.out.println("Closed all the browsers");
}
#Test
public void visitGoogle(){
driver.get("https://google.com");
try {
int a=1/0;
}
catch(Exception ex){
ex.printStackTrace();
driver.quit();
}
}
Hope it will help you
Small update: there is an option in testNG for annonations i.e. beforetest, aftertest. And that option is #AfterTest(alwaysRun = true)...once this is set then the #AfterTest will always execute even if there was exception in the methods before.

Error in selenium webDriver while executing the normal test case

Here is my code for selenium webDriver
package com.pack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchTest {
public static void main(String...args) {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
String appTitle = driver.getTitle();
System.out.println("Application title is :: "+appTitle);
driver.quit();
}
}
Below is the Error I am getting:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at com.pack.GoogleSearchTest.main(GoogleSearchTest.java:9)
I need to understand why I am getting that error.
Looks like you are trying to run the code that is already having compilation issues. You should be seeing a tab called "Problems", where it shows all/ any possible errors and warnings. Do you see anything in that tab? If so it will walk through the steps that you need to follow to overcome the issue.
The other option is to download a fresh copy of eclipse IDE and configure your selenium. Let me know which one worked for you.

driver.close() method is not working in Selenium WebDriver on Firefox

I am writing a simple program in Eclipse using JUnit annotation.
diver.close() is not closing my Firefox browser after the tests. It works fine with Chrome. Code snippet is here.
public class FireFox1 {
WebDriver driver;
#Before
public void setUp() {
driver= new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter4");
}
#After
public void tearDown() {
driver.close();
}
#Test
public void testExamples() {
WebElement element= driver.findElement(By.id("nextBid"));
element.sendKeys("100");
}
}
sometimes while on repeated usages,we'll facing problems with driver.close().
Anyways driver.quit() will rectify your problem.
Generally driver.close() closes the browser(the instance of driver is still intact) and driver.quit() is to kill the webdriver instance. As anyhow you are using here for only one page,then you can go with driver.quit().
Thank you.
Better use driver.quit() method. It closes the browser, but due to some unknown issues it throws NullPointerException. Just catch it..
try{
driver.quit();
}catch (Exception e){
System.out.println("Nothing to do with it");
}
Assuming you have started 5 browsers(classes) parallely using grid:
driver.close - Used for close the current browser( where execution going on)
driver.quit - Used for close all the browsers started by the current execution.
You can use any one of this..
May be browser compatiblity issue, try to downgrade the FF let we see...
Use latest GeckoDriver.exe (17) with Latest FireFox (54.0);
It works fine for me. I had the same problem before.
This problem that you are facing is completely a compatibility problem between driver & Browser version.
driver.close(); should have work without problem if you use above versions. Let me know if it works.
This is what the problem in Firefox driver.close() works in Firefox only with internet connection but in case of Chrome it works without internet connection.

Unable to work with Google chrome driver

I want to work with google chrome. I am using the following code to instantiate google chrome driver object. But, when I click "System." and doing ctrl+space for suggestions, its showing "No Default Proposals".
and when writing the full code, it's showing error. See picture.
There is no issue with the chrome driver. The problem is you're trying to use it under the class level and not inside a method
Place the code inside any method and you will not get any errors.
I guess you are missing the annotations/method here
something like this
public class Chromedriver{
WebDriver driver;
#BeforeClass
public void setup() {
System.setProperty("webdriver.chrome.driver", "Path to chrome driver");
driver=new ChromeDriver();
driver.get("https://accounts.google.com/");
}
Or call the above in main method or a seperate method. Please make sure all the imports are done.
Cheers!!!
You need to import the Chrome driver class as well for using selenium with Chrome.
Please try this code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Tester {
private static WebDriver driver = null;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path to chrome driver");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.example.com");
driver.quit();
}
}