Selenium webdriver running issue - selenium

I am trying to running selenium webdriver using eclipse.Unfortunately i face the following issue:
click here for the snapshot
and the code is,
package myproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "K:\\New folder\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(); driver.get("http://only-testing-blog.blogspot.in");
String i = driver.getCurrentUrl();
System.out.println(i); driver.close();
}
}

UPDATE:
If you right click on your project and "Run as... Java application" and you have multiple main methods in your project, eclipse doesn't know which one to start so it asks which class. The solution is to right click on your class and "Run as...". After that you can just press "Run".

Related

Exception in thread Unresolved compilation problems:

This is my script:
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.close();
}
}
When I run the script, am getting below error, I tried to add the java lib in Class path instead of Module path, still the issue not resolved, someone please help;
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 sampleTests.AdminLogin.main(AdminLogin.java:10)
You have to set the path of the Geckodriver (the executable that is used for Firefox tests using Selenium).
Search for Geckodriver in Google.
Download the executable as per your system (Win/Mac/Linux)
Set the path of the executable in your tests as given below -
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",Path_of_Firefox_Driver");
// example for Windows
System.setProperty("webdriver.gecko.driver",D:\\GeckoDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://testautomationu.applitools.com/");
driver.quit();
}
}

Error while invoking Internet Explorer browser using Selenium

Can anyone please help with the Selenium code below. I am getting an error while invoking Internet Explorer for automation testing.
Code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver","C:\\microsoftwebdriver\\MicrosoftWebDriver.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
}
Error screenshot attached:
InternetExplorerDriver
InternetExplorerDriver class is the WebDriver implementation that controls the IEServerDriver and and allows you to drive Internet Explorer browser running on the local machine. This class is provided as a convenience for easily testing the InternetExplorer browser. The control server which each instance communicates with will live and die with the instance.
To create a new instance of the IEServerDriver you need to use the IEServerDriver binary instead of MicrosoftWebDriver.exe which you need to download from selenium-release.storage, unzip and provide the absolute path within the System.setProperty() line. So your effective code block will be:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver","C:\\path\\to\\IEServerDriver.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
}

TestNG error via command line : Cannot instantiate class testCases.LoginPage

Cloned the project from github at location :
C:\Automation\CC_Regression_Automation\CC_Regression
When trying to run testng.xml using eclipse all goes well.
Getting Cannot instantiate class testCases.LoginPage when trying to run the code using command line.
**Loginpage.java**
package testCases;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.DriverUtil;
import utils.Loggers;
import utils.ReportGenerator;
import utils.WebPageUtils;
public class LoginPage extends Base{
/*Members of the current Test Class. The number varies from script
to script depending on the variables and verifications required*/
public WebDriver driver;
private String currentSitePath;
private String testCaseName=getClass().getName().substring(10);
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page");
//Function to Navigate to a particular URL inside CC
public void navigateToURL(WebDriver driver){
siteURL="";
this.driver.navigate().to(baseurl+siteURL);
}
#Test // Main Test Flow for the Script
public void executeScript() throws IOException{
System.out.println("*******************");
System.out.println("launching chrome browser");
//Test Case Author assignment in Reports
ReportGenerator.assignAuthor(parentTest,"Garima");
//Setting up Browser Instance
this.driver=driverIns();
//Navigating to the required page in CC
navigateToURL(this.driver);
Sleep(5000);
String strPageTitle = this.driver.getTitle();
System.out.println(strPageTitle);
//Start Logs
Loggers.startCurrentTestCaseExecution(this.driver);
try{
ReportGenerator.verifyNavigation(this.driver, "Control Center", parentTest,testCaseName,"Yes");
Code snippent of Base.java
//Function to instantiate the WebDriver instance based on the Browser
selected for Windows
public WebDriver driverInsWindows(){
isExtensionEnabled();
setbaseURL();
try {
switch(getBrowser()+isExtensionEnabled.toString()){
case "Chromefalse":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=new ChromeDriver();
driver.get(baseurl);
break;
case "Chrometrue":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=invokeChromeBrowserwithExtension();
driver.get("https://www.google.com");
break;
case "Internet Explorer":
System.setProperty("webdriver.internetexplorer.driver",
"./Win/Drivers/internetexplorerdriver.exe");
driver=new InternetExplorerDriver();
driver.get(baseurl);
break;
case "Firefox":
driver=new FirefoxDriver();
driver.get(baseurl);
break;
default:
//new PascalBaseClass();
}
} catch (IOException e) {
}
Executing below command via cmd:-
java -cp C:\Automation\CC_Regression_Automation\CC_Regression\bin;C:\Automation\CC_Regression_Automation\CC_Regression\lib\* org.testng.TestNG testng.xml
Blockquote
Your problem might be this line:
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page")
Since I'm seeing other references to ReportGenerator
I doubt you typed all that code, and it's a copy/paste so I'm not sure why your IDE doesn't flag it, or is there somehow another object called ReortGenerator

Cannot automated sign in for dropbox.com through Selenium (Java)

Hello I am new to automation and i have tried automating dropbox.com sign in but my code is failing after clicking on sign in link. i am not able to pass the values (username and password) in the frame of the sign in box. Following is my code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Drop_box {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.dropbox.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//*[#id='pyxl1851']")).sendKeys("123#gmail.com");
driver.findElement(By.xpath("//*[#id='pyxl1854']")).sendKeys("1234");
driver.findElement(By.xpath("//*[#id='regular-login-forms']/form[1]/div[3]/button")).click();
}
}
This should work:
driver.findElement(By.cssSelector("a#sign-in")).click();
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_email']")).sendKeys("email");
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_password']")).sendKeys("password");
driver.findElement(By.cssSelector("div#index-sign-in-modal div.sign-in-text")).click();
If you prefer xpath over css selectors, then use following lines of code:
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_email']")).sendKeys("email");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_password']")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//div[#class='sign-in-text']")).click();

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();
}
}