Selenium Maven Setup - One Empty Jar - java

I'm trying to set up a Java Selenium test using the recommended Maven instructions found here:
http://docs.seleniumhq.org/docs/03_webdriver.jsp
and here:
http://docs.seleniumhq.org/download/maven.jsp
I have maven installed and working.
I've copied the example pom.xml, changing only the project name
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SeleniumTest</groupId>
<artifactId>SeleniumTest</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Using
mvn clean install
runs without any errors. The target directory is created, containing SeleniumTest-1.0.jar and the maven-archiver directory. The problem is that my Eclipse project can't resolve the Selenium classes. I've copied the example Java driver class, modifying the imports based on my project layout:
import Selenium.*;
import Selenium.target.*;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("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());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
The classes "WebDriver", "WebElement", "WebDriverWait", and "ExpectedCondition" can't be resolved. Trying to use the imports in the example
import Selenium.By;
import Selenium.WebDriver;
import Selenium.WebElement;
import Selenium.firefox.FirefoxDriver;
import Selenium.support.ui.ExpectedCondition;
import Selenium.support.ui.WebDriverWait;
all fail.
I looked into the jar downloaded by Maven, SeleniumTest-1.0.jar, and it is effectively empty. It only contains the META-INF directory.
I feel like I'm missing something obvious, but I just can't figure it out. I feel like I'm missing something in my pom.xml, but I can't find anything on Selenium's site that helps. Can anyone give me a hand?

Related

error on code " org.openqa.selenium.remote.DriverCommand.NEW_SESSION"

I am getting below error with my java program:-
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.remote.DriverCommand.NEW_SESSION(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/remote/CommandPayload;
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:211)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:147)
at automation.flyYatra.mainTest(flyYatra.java:42)
at automation.flyYatra.main(flyYatra.java:243)
Tried with adding the latest jar files as can be seen the snapshot below, but could not resolve it. Any help, please?
package automation;
public class flyYatra {
public void mainTest() throws IOException, InterruptedException, WebDriverException, SocketException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\Driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Tried with various versions of jar files, but could not solve
Your various versions of jar files is the problem known as JAR Hell because in the CLASSPATH there are multiple libraries having the same classes and the order of classloading varies depending on the underlying operating system and location of the libraries.
Make sure to have the same version of the Selenium Java libraries, to be more specific the latest one is 3.141.59
So I would recommend going for a dependency management solution like Apache Maven, you should get the things some using this small pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-java</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
Save it somewhere on your disk and execute mvn dependency:copy-dependencies command - it will download a bunch of "good" .jars to "target/dependencies" folder.
You can also use this repository mentioned in Selenium with Java article as a basis for your tests

How to run jsoup code from maven in Eclipse?

I created a new maven project in Eclipse and created a file Main.java under src/main/java/parser. Here the package is parser in which my Main.java file is located. Here are the contents of Main.java, which is an example from Jsoup website.
package parser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
System.out.println(doc.title());
Elements newsHeadlines = doc.select("#mp-itn b a");
for (Element headline : newsHeadlines) {
System.out.println(headline.attr("title") + "\n\t" + headline.absUrl("href"));
}
}
}
Here is the pom.xml file:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>mdlparser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- jsoup HTML parser library # https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
</project>
Now, how can I run this project? I right clicked pom.xml file and hit Maven Build which opened run configuration window. What am I supposed to type here? I tried typing package and eclipse:eclipse. In both cases, the console's output says that the build was successful, but the output from my program(System.out.println) is not being shown in the console. Also I tried running it as simple java application but in that case I get NoClassDefFoundError.
Note: I know that you usually use a logger instead of using sysout. But I am really confused as to how to run this simple hello world like program.

"not all expectations were satisfied" missing under Eclipse

We use two different IDEs, Netbeans 8.2 and Eclipse 4.7.2. We are running JMock 2.8.3 with JUnit 4.11 and have a test that fails under Netbeans and Jenkins (using the Netbean's Ant scripts), but passes under Eclipse.
The error is "not all expectations were satisfied".
However, if I add an assertIsSatisfied() call to the end of the test, it will fail with the correct error message under Eclipse.
I can reproduce this with a trivial example:
public class FailureExample {
private static class Example {
public void doSomething() { }
public void doSomethingElse() { }
}
// Mocks
#Rule public JUnitRuleMockery context = new JUnitRuleMockery(){{
setThreadingPolicy(new Synchroniser());
setImposteriser(ClassImposteriser.INSTANCE);
}};
public Example instance;
#Before
public void setUp() throws Exception {
// Mocks
instance = context.mock(Example.class);
}
#Test
public void testExample() {
context.checking(new Expectations() {{
oneOf(instance).doSomething();
oneOf(instance).doSomethingElse();
}});
instance.doSomething();
}
}
Is there something else I need to do in Eclipse to make JMock behave as expected?
Update
Adding screenshot of our project's libraries:
UPDATE
I tried create a new Java project as well as a new Maven project (as described below by Till Brychcy) as those worked. I tried removing all the jar files listed for my project and then readding them, but it failed.
I'm very close to abandoning Eclipse in favor of Netbeans, simply because I have real work to do, not just fighting with Eclipse.
I cannot reproduce your problem.
With both Eclipse 4.7.2 and Eclipse built from the current master I get:
java.lang.AssertionError: not all expectations were satisfied
expectations:
expected once, already invoked 1 time: example.doSomething()
! expected once, never invoked: example.doSomethingElse()
what happened before this:
example.doSomething()
I used the following pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>jmockbug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jmockbug</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>

Robot framework not picking up keyword implementation

I'm trying to set up robot on top of an Eclipse Maven-Selenium-TestNG java project I created, but it doesn't seem to be picking up default keywords (I haven't even tried adding my own yet).
I started by creating a maven project and adding to pom.xml the dependencies for selenium 3.4, testNG 6.8 and robot 3.0.2, then also added robot plugin 1.4.7. Finally, updated the project so maven downloads all the needed stuff.
To test selenium (without robot) I created a textNG class in src>test>java, added a system property pointing to the chromedriver.exe file in my system and added a simple test that just opens the browser and navigates to google. It worked, so now I want to use robot on top of that.
This is my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.automation</groupId>
<artifactId>automated_tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robotframework</groupId>
<artifactId>robotframework</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.robotframework</groupId>
<artifactId>robotframework-maven-plugin</artifactId>
<version>1.4.7</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I created a file in src/test/robotframework/acceptance, with the following contents:
*** Settings ***
Test Set Up Start Selenium Server
Test Tear Down Stop Selenium Server
*** Test Cases ***
Visit google
Open Browser https://www.google.com chrome
Close Browser
However, when I run as maven install, I get:
Setup failed: No keyword with name 'Start Selenium Server' found.
Also teardown failed: No keyword with name 'Stop Selenium Server'
found.
So why is it that robot is not finding the keywords implementation? And how do I add implementations of my own keywords?
The reason robot isn't finding the keywords is that you aren't importing the library that contains the keywords. Start Selenium Server is part of the deprecated SeleniumLibrary. In order to use the keywords you must import them with the Library setting:
*** Settings ***
Library SeleniumLibrary
Test Set Up Start Selenium Server
Test Tear Down Stop Selenium Server
Assuming that the folder where SeleniumLibrary is installed is in your PYTHONPATH, robot will import the library and make the keywords available to you.
I actually found out I was missing a maven dependency:
<dependency>
<groupId>com.github.markusbernhardt</groupId>
<artifactId>robotframework-selenium2library-java</artifactId>
<version>1.4.0.8</version>
</dependency>
Also, I don't need to use Start Selenium Server and Stop Selenium Server because they're deprecated.
After that, I was able to run my test by creating a custom keyword to set browser path (I'm using chromedriver):
I created a .java file within src/main/java/demo and added a method that sets up the property:
package demo;
public class Setup {
public void driverPath() {
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
}
}
Then, I created src/test/robotframework/acceptance/Resource.robot file and imported my library:
*** Settings ***
Library Selenium2Library
Library demo.Setup
Also created a src/test/robotframework/acceptance/__init__.robot file and used the keyword I created (Browser Setup):
*** Settings ***
Test Setup Driver Path
Test Teardown Close All Browsers
Test Timeout 2 minute 30 seconds
In my test, I invoked Resource.robot:
*** Settings ***
Resource Resource.robot
*** Test Cases ***
Visit google
Open Browser https://www.google.com chrome

Error when adding selenium android driver jar in TestNG. Cannot find class file?

I'm writing a test class for an Android App, but I keep getting the error code "The project was not built since its build path is incomplete. Cannot find the class file for org.openqa.selenium.html5.BrowserConnection. Fix the build path then try building this project." each time I try to add the selenium Android driver jar file to the project. I need the Android driver to use the TestNG with Appium properly, otherwise Appium can't find elements on the page when testing. The Code:
package appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class TestCase {
public class NewTest {
WebDriver driver = new AndroidDriver();
#BeforeClass
protected void setUp() throws MalformedURLException {
//Tells the program what device is used
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.2.2");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");
//Tells the program what app is being run
capabilities.setCapability("appPackage",
"com.android.testapp2");
capabilities.setCapability("appActivity",
"com.android.testapp2.MainActivity ");
driver = (AndroidDriver) new RemoteWebDriver(new
URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void test() {
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("a");
WebElement button = driver.findElement(By.id("btnEnter"));
button.click();
};
#AfterClass
protected void tearDown() throws Exception {
driver.quit();
}
}
}
Here's the code of the pom.xml file I'm trying to edit:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestProject</groupId>
<artifactId>TestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source/>
<target/>
</configuration>
</plugin>
</plugins>
</build>
</project>
I don't see anywhere to put a dependency, and I'm not even sure if that's what I'm supposed to be doing.
Ok, this should get you going again, I think.
The improvements made in the pom.xml:
No dependencies where definded, so added the required onces
testng v6.9.8
selenium-java v2.39.0
selenium-android-driver v2.39.0
Remark 1: The Selenium Java and Android libraries, should be of the same version (for as far as I know, otherwise it can give issues).
_Remark 2:_The Selenium Android library, is an older library. It should actually be replaced by the newer one Selendroid. Therefor also some import would change.
Improved the <groupid> and <artifactid>, these are standard in lower case, and should represent some logical and unique thing. Maybe you store you project on bitbucket, or github, then a logical group id would start with com.github.<your-github-account-id>.<name-of-the-project>.
Removed the <sourceDirectory>src</sourceDirectory>
Created a directory src/test/java
Added the package name appiumTest to that test source directory
added the class TestCase in that package.
The Maven project file pom.xml:
Remark Below is the correct Maven starting tag <project> shown, as it should be used in the pom.xml. But StackOverflow, does not show it correctly, when the starting tag has attributes.
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Below the corrected pom.xml, but without the correct starting tag.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.organisation.project</groupId>
<artifactId>android-selenium</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<!--
<version>2.48.2</version>
-->
<version>2.39.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-android-driver</artifactId>
<version>2.39.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source />
<target />
</configuration>
</plugin>
</plugins>
</build>
</project>
The improvements made in the class TestCase:
No need to use of inner class NewTest, so removed it.
Removed initialization of field driver, as it gets initialized in the #BeforeClass method.
Added check in the #AfterClass, only call driver.quit(), when driver is initalize.
The class TestCase:
package appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
public class TestCase {
WebDriver driver; // = new AndroidDriver();
#BeforeClass
protected void setUp() throws MalformedURLException {
// Tells the program what device is used
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.2.2");
capabilities.setCapability("deviceName", "Emulator");
capabilities.setCapability("platformName", "Android");
// Tells the program what app is being run
capabilities.setCapability("appPackage", "com.android.testapp2");
capabilities.setCapability("appActivity",
"com.android.testapp2.MainActivity");
driver = (AndroidDriver) new RemoteWebDriver(new URL(
"http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void test() {
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("a");
WebElement button = driver.findElement(By.id("btnEnter"));
button.click();
};
#AfterClass
protected void tearDown() throws Exception {
if (driver != null) {
driver.quit();
}
}
}
Possible Improvements
Correct the package name (currently appiumTest), but that is not inline with the nameing convention, all lower case, seperate with a dot .. And something meaningful, similar as to the Maven group-id. Sp start with com.github.<your-github-account-id>.<name-of-the-project>
Give the class TestCase a better name
Update to Selendroid, then also update the Selenium Java version

Categories

Resources