When I run the code, I get the following error:
Cannot instantiate class mypackage.GoogleSearch
How can it be fixed?
Code:
package mypackage;
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearch {
public String url = "http://www.google.com/";
public WebDriver driver = new FirefoxDriver();
#Test
public void search() {
driver.get(url);
}
}
I got it working, apparently I needed to install the stand-alone server: https://code.google.com/p/selenium/downloads/detail?name=selenium-server-standalone-2.39.0.jar&can=1&q= If anyone could explain why it'd be great, with Visual Studio I just had to install selenium client
Related
I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class NewTesting {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() {
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more?
I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!
EDIT:
I now have this:
public class NewTest {
private WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
It does open the website now but I'm getting a nullpointer exception now:
FAILED CONFIGURATION: #AfterTest tearDown
java.lang.NullPointerException
at NewTest.tearDown(NewTest.java:21)
Replace this set of imports:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
With:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.
Your effective code block will be:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class NewTesting {
WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
If you're on windows, this previous question may be some help to you.
It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
I just started learning Selenium.
This is my code:
package sele;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Abc {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.close();
}
}
This is the exception:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:247)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:242)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:238)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:127)
at sele.Abc.main(Abc.java:10)
I am not sure if it is the same in java but in C# you use:
driver.Quit();
at the end of your code. Some use
driver.Dispose();
This will work as well.
Look here for more Help.
i'm trying to run selendroid web for practice but i'm getting errors even i update selenium version (2.48.2) and set all paths correctly.
below is my code and i'm getting this error.
Please help me...
NoSuchMethodError: org.openqa.selenium.remote.CommandInfo.(Ljava/lang/String;Lorg/openqa/selenium/remote/HttpVerb;)V
package com.guru.test;
import io.selendroid.SelendroidCapabilities;
import io.selendroid.SelendroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class testWeb {
public WebDriver driver;
#BeforeSuite
public void setUp() throws Exception
{
DesiredCapabilities caps = SelendroidCapabilities.android();
driver = new SelendroidDriver(caps);
}
#Test
public void WebSiteTest() throws Exception
{
driver.get("http://google.com");
WebElement searchQuery = driver.findElement(By.name("q"));
searchQuery.click();
searchQuery.sendKeys("Test");
WebElement submit = driver.findElement(By.name("btnG"));
submit.click();
}
#AfterSuite
public void tearDown() throws Exception{
driver.quit();
}
}
I think you have mixed up selenium and selendroid jars. selendroid-standalone + selendroid-client should be enough for this.
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();
}
}
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();
}
// ....
}
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
Hi I am new to Jenkins.
I have configure the Jenkins locally on my machine and Its running fine.
I need to ask whenever my Integration tests (written in Junit) are passed, Jenkin doesn't stops the build and its continue.
But in logs It displays the test cases are passed and no errors are found.
Could some one please suggest any solution how to stop jenkins build?
My Code:
package com.workshop.airport.workshop.airport;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
public class PageStepsDefs {
public String ChromeDriverPath="C:\\Users\\zain.jamshaid\\Desktop\\chromedriver.exe";
public WebDriver webdriver;
String localhost="http://www.google.com";
public PageStepsDefs (){
System.setProperty("webdriver.chrome.driver",ChromeDriverPath);
webdriver = new ChromeDriver();
}
#Given("^I browse to the (.+) page$")
public void open_page(String url)
{
webdriver.get(localhost+url);
System.out.println(localhost+url);
}
#When("^I click on the button (.+)$")
public void click_On_Menu(String Id)
{
webdriver.findElement(By.id(Id)).click();
System.out.print(Id);
}
#After
public void close_browser(){
webdriver.close();
}
}
I have also attached the screenshot of jenkins console logs
Any help will be awesome.
Thanks!
I was using webDriver.close();
but
webDriver.quit();
fix my problem
This fixed the problem