I tried to implemented a web service from a dynamic web project. I added the selenium-server-standalone-2.32.0.jar file to the buildpath, then I also added it to the WEB-INF/lib folder. Then I used the web service wizard to generate the web service from the project. At the start of the wizard it displayed a pop-up warning that read:
The service class "test.eko3.TestEko3" does not comply to one or more requirements of the JAX-RPC 1.1 specification, and may not deploy or function correctly.
The value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" does not have a public default constructor. Chapter 5.4 of the JAX-RPC 1.1 specification requires a value type to have a public default constructor, otherwise a JAX-RPC 1.1 compliant Web service engine may be unable to construct an instance of the value type during deserialization.
The field or property "windowHandles" on the value type "org.openqa.selenium.WebDriver" used via the service class "test.eko3.TestEko3" has a data type, "java.util.Set", that is not supported by the JAX-RPC 1.1 specification. Instances of the type may not serialize or deserialize correctly. Loss of data or complete failure of the Web service may result.
I clicked ok and it generated the web server and the client and displayed the client in the eclipse's browser. But when I entered the parameters and clicked invoke it displayed this exception in the result section:
Exception: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver Message: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver; nested exception is: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
Since I added the selenium jar into both the buildpath and the WEB-INF/lib folder I'm not sure why it can't find the class. The code for the server is below:
package test.eko3;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestEko3 {
public String Ekobilet(String from, String to, String date) {
//Firefox browser instantiation
WebDriver driver = new FirefoxDriver();
//Loading the URL
driver.get("http://www.amadeusepower.com/trek/portals/trek/default.aspx?Culture=en-US");
WebElement radioOneway = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_rbFlightType_1"));
radioOneway.click();
waitForPageLoaded(driver);
WebElement fromText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtFrom"));
fromText.clear();
fromText.sendKeys(from);
WebElement toText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtSearch_txtTo"));
toText.sendKeys(to);
WebElement dateText = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_txtDepartureDate_txtDate"));
dateText.sendKeys(date);
//Sign in button identification and click it
WebElement searchbutton = driver.findElement(By.id("ctl00_ctl00_ctl00_cph1_cph1_QuickSearchAll1_QuickFlightSearchControl1_btnSearch"));
searchbutton.click();
String page = driver.getPageSource();
// Writer out = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream("ekobiletselenium.html"), "UTF-8"));
// try {
// out.write(page);
// } finally {
// out.close();
// }
//Closing the browser
driver.close();
return page;
}
public static void waitForPageLoaded(WebDriver driver) {
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
Wait<WebDriver> wait = new WebDriverWait(driver,30);
try {
wait.until(expectation);
} catch(Throwable error) {
System.out.println("exception yavrum");
}
}
}
Can someone please tell me the cause of this? Am I missing a jar file that selenium depends on? Any help would be appreciated.
Maybe you are using the standalone jar which is not good for web projects.
See http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html
This exception appears when selenium is not fully referenced in the project.
Here are the steps you can take to resolve the issue:
Download the Selenium Client & WebDriver Java Bindings here.
Unzip the downloaded file.
Copy the main selenium '.jar' (example: selenium-java-2.42.2.jar) to the web project's WebContent/WEB-INF/lib dir.
Also copy All the .jar files the Unzipped selenium libs dir to the web project's WebContent/WEB-INF/lib dir.
You should now be able to run the code without getting the java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver exception.
Please check this link definitely you can get the idea which is answered by #thedrs
http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html
Related
I have a cucumber project. If I want to take screenshots I want to embed it using the following.
scenario.embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES), "image/png"); however I get an error with embed - Cannot resolve method 'embed' in 'Scenario'
Part of my hooks file
#After
public void teardownAndScreenshotOnFailure(Scenario scenario){
try {
if(driver != null && scenario.isFailed())
{
scenario.embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES), "image/png");
}
if(driver != null)
{
driver.manage().deleteAllCookies();
driver.quit();
driver = null;
}
....
I have imported the following:
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
I am using the latest version of cucumber in my POM.xml : 6.9.1
I am not sure why I get this issue, Ive tried downgrading my cucumber, google the error but shomehow embed is not working.
As per the Java documentation for 6.9.1 . You can use attach() method.
embed() method was deprecated and its removed from 6.0 Documentation.
public void attach(byte[] data, String mediaType, String name)
Attach data to the report(s).
// Attach a screenshot. See your UI automation tool's docs for
// details about how to take a screenshot.
scenario.attach(pngBytes, "image/png", "Bartholomew and the Bytes of the Oobleck");
To ensure reporting tools can understand what the data is a mediaType must be provided. For example: text/plain, image/png, text/html;charset=utf-8.
Parameters:
data - what to attach, for example an image.
mediaType - what is the data?
name - attachment name
Executing simple class
Enter code here
package lesson1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Brf {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
}
Getting the error:
Error on simple class in selenium webdrive:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:335)
You are getting this error because you haven't set the path of gecko driver in your code.
Please set the path of gecko driver before creating a new instance of Firefox driver.
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
Need to add driver path, if you are using selenium jars above 3.0 , if you are using selenium jars below 3.0 your code is good to go, but i guess you are using selenium jars above 3.0
Public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver"," Path to geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/login.php");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful -Passed");
}
else
{
System.out.println("Login Unsuccessful -Failed");
}
driver.close(); //Closes the Browser
}
The cause of the error is that you don't have a proper setup for detection of a GeckoDriver file named "geckodriver.exe" (this prevents you from being able to open Firefox with your program). In case you don't have such a file, you can download one h̲e̲r̲e̲ (I suggest getting the latest version).
With such a file, you can carry out one of the following two procedures to rectify your problem:
Method 1
Add the path of the folder containing the "geckodriver.exe" file to your PATH environment variable value (instructions on how to modify this variable value can be seen h̲e̲r̲e̲).
Method 2
Prepend the body of your main method with the following statement (where GECKODRIVER_PATH represents the path of the "geckodriver.exe" file):
System.setProperty("webdriver.gecko.driver", "GECKODRIVER_PATH");
Set the gecko driver path which is initialise the firefox driver.
I have a maven-powered Robot-framework project in java that uses selenium 3.4.0 dependency, robotframework 3.0.2 dependency, markusbernhardt's selenium2library version 1.4.0.8, and robotframework-maven-plugin version 1.4.7.
My robot tests live in the src/main/test/robotframework/acceptance folder, while in src/main/java/mypackage I created a Customized.java file to set a system property for the browser driver path (I then import this library in my tests:
*** Settings ***
Library Selenium2Library
Library mypackage.Customized
This works perfectly. But now I'd like to implement my own keywords to extend Selenium2Library. But I'm not sure how to obtain the current WebDriver instance that is running.
I mean, if I weren't using Robot and just plain Selenium, I'd do something like this:
WebDriver driver=new ChromeDriver();
driver.doSomething();
However, in this case I don't want to instantiate a new WebDriver but instead get the one that is currently running (which Robot automatically instantiated). How can I do that?
I've so far created a Selenium2Library object and set it with the value returned by Selenium2Library.getLibraryInstance(); but that's not giving me access to selenium's methods (e.g.: getCurrentUrl() is not listed).
in python it can be done with following code
from robot.libraries.BuiltIn import BuiltIn
def _browser(self):
return BuiltIn().get_library_instance('Selenium2Library')._current_browser()
Actually, I found a solution, but I'm not sure it's the right approach:
public class Customized {
private static Selenium2Library s;
private BrowserManagement b;
private WebDriver driver;
public Customized() throws ScriptException {
try {
Customized.s = (Selenium2Library) Selenium2Library.getLibraryInstance();
} catch (javax.script.ScriptException e) {
e.printStackTrace();
}
b = s.getBrowserManagement();
driver=b.getCurrentWebDriver();
}
}
Now the selenium methods are available from the driver object. However, there might be a better way to do it, as there's this message in the javax.script.ScriptException exception: "Access restriction: The type 'ScriptException' is not API (restriction on required library 'C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar')"
For newer Selenium version (4.0) the following snippet can be used:
class SelCustomLib():
def get_browser_driver(self):
return BuiltIn().get_library_instance('SeleniumLibrary').driver
I'm new to JAVA and Selenium, I installed both Eclipse and Selenium webdriver and I'm trying my first example (search a keyword in Google):
package testproject;
public class testclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
Object driver;
//Open Home Page
((Object) driver).get("http://www.google.com");
//Enter text in search box
driver.findElement(By.name("q")).sendKeys("selenium");
Thread.sleep(1000);
//Click Search button
((By) driver).findElement(By.name("btnG")).click();
Thread.sleep(10000);
}
}
I got the following exception:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: The method get(String) is undefined for the type Object By
cannot be resolved By cannot be resolved to a type By cannot be
resolved at testproject.testclass.main(testclass.java:10) Picked up
JAVA_TOOL_OPTIONS: -agentlib:jvmhook**
you should import the selenium some class ,you can use the some methods and you ought crate a drive instance
Use org.openqa.selenium.WebDriver instead of Object. If you cannot import it, download the Selenium Standalone Server from http://docs.seleniumhq.org/download/ and add it to your external libraries.
In Eclipse you can add it with click right to your project > properties > Java Build Path > Add External Jars.
You also have to create the WebDriver Object easiest with
WebDriver driver = new FirefoxDriver();
I am leaning selenium. I have trouble to run a sample script. Please help me to figure out what I did wrong. Thanks!
My installations:
JDK1.7.0._02
selenium-server-standalone-2.31.0.jar
Eclipse IDE 3.7.0
Selenium IDE 1.9.0 (Firefox plugin)
Eclipse indicates the following error message when I click on the package section in the code
1.the declared the package org.openqa.selenium.example; doesn't match expected package Seletest1
2. syntax error on token package, imported expected
Eclipse also suggested move Test1.java to package 'org.openqa.selenium.example
Please suggest the right action for me to take, should I imported org.openqa.selenium.example into the build path of my project or should I move Test1.java into the package?
where Can I find out the location of the package-org.openqa.selenium.example?
Here is my code copied from Google code get started with Selenium
my project structure SeleniumTest1>Src>SeleTet1
package SeleTest1;
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Test1
{
public static void main(String[] args)
{
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
}
The error message showed when I execute the code
When I run the code in Eclipse, I received the following error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)
You have a duplicate package declaration on the top of your code. I would remove the second one (org.openqa.selenium.example) since your code is probably in the SeleTest1 folder.
Your package declaration is not required to match the one of the framework you are using.
When you export the selenmium IDE recorded test case into webdriver format, by default the package statement will get added as
package org.openqa.selenium.example;
We need to modify it as per our package name created in Eclipse.
So, in your case, you can remove the below duplicate line.
package org.openqa.selenium.example;
You will not get the 2nd error also once you have done this modification.