Running a WebDriver Test without using ANT, Maven, JUnit or Eclipse - selenium

I would like to know if it's possible to run a WebDriver test in Java using just a plain text editor, Firefox browser, Java SDK and WebDriver JAR?
I am trying to come up with the most "bare-metal" way to run a test without adding Test Runners, Test Frameworks, Dependency Managers into the mix.
I understand that this is not the "right" way to do things, but I am trying to find a way to create a new kind of WebDriver tutorial that will focus only on the API.
I am using OS X right now but instructions for Windows would be equally appreciated.

Running WebDriver is as simple as
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
The only thing that you need is a correct classpath, which is just selenium-java-2.38.0.jar and supporting libraries, namely: guava-15.0.jar json-20080701.jar commons-exec-1.1.jar httpcore-4.3.jar httpclient-4.3.1.jar commons-logging-1.1.1.jar
Or, as per JimEvans, you can download standalone selenium-server-standalone-2.40.0.jar that has all dependencies included.

Firstly record and run the script you want to do then export it as junit4 test suite and save. Second copy the full code and aste it in eclipse then you can get the option "Run as junit4 test" while running code.

Related

Selenium - Automated Testing and Edge

Can someone point me to a website or information that discusses how to setup Selenium and write automated scripts? I need to write scripts to test a web based application. I know how to use Java and would like to do that approach.
Thanks
From the description, it looks like you want to automate the MS Edge (Chromium) browser using Selenium web driver.
I suggest you refer to the steps below.
Download the Java binding of Selenium 4.0.0-beta-1 from here.
Download the matching version of Microsoft Edge Driver from this page.
Create a new JAVA project and try to add a reference to the Java binding files by Right-click the project folder-> Build path-> Configure Build path-> Libraries tab-> Add External JARs.
Add the code below to your JAVA project.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
public class test {
public static void main(String[] args)
{
System.setProperty("webdriver.edge.driver", "Edge_web_driver_path_here....\\msedgedriver.exe"); //Modify path here...
EdgeOptions edgeOptions = new EdgeOptions();
//edgeOptions.addArguments("-inprivate");
WebDriver driver = new EdgeDriver(edgeOptions);
driver.get("https://Website_URL_here..."); //Modify URL here...
//--------------------------------------
//Your_Automation_code_here....
//--------------------------------------
driver.quit();
}
}
Run the code.
Output:
Further, you can try to modify the code example as per your requirements.

Edge WebDriver Version

How to you tell which version of Edge web driver you are using?
I want to use the web driver Microsoft Edge 44.18362.449.0, Microsoft EdgeHTML 18.18363
as instructed here I did this to get the Webdriver
"Install via DISM by running the following command in an elevated command prompt: DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0"
I am using selenium with java, I have tests working with chrome browser selenium and I access the web driver by using .exe file however no .exe file was provided, or I am unaware where it was install. I looked in the directory I was in when I ran the command above and no exe was found.
please advise on how i can connect to the web driver using java and selenium.
You should find the MicrosoftWebDriver.exe' atC:\Windows\System32\MicrosoftWebDriver.exe` as shown in the below screenshot.
Yes, I agree with the suggestion given by #supputuri.
You can also directly reference the path in a code like below. It will also work.
Example:
package selenium_test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.*;
import org.openqa.selenium.WebElement;
public class new_java_class
{
public static void main(String[] args)
{
System.setProperty("webdriver.edge.driver","C:\\\\Windows\\\\System32\\\\MicrosoftWebDriver.exe");
WebDriver browser = new EdgeDriver();
browser.get("https://microsoft.com");
}
}

Do I need to change config settings to use Selenium Grid for running multiple cucumber-jvm feature files?

I have a maven test project which uses Cucumber-jvm internally. I want to run my feature files in parallel using Selenium-Grid 2.0.
I have started the hub and node but when I am running my tests. It is running tests sequentially in only one chrome instance. Though I can see 4 instances of chrome on my hub.
Below is my #Before hook.
#Before
public void beforeScenario() throws Exception{
//grid code
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.MAC);
cap.setCapability("version", "41");
driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
cap);
endUser.is_the_login_page();
}
Some help will be useful.
Thanks
Are you using one Test class or multiple? A single test class is going to be run single-threaded. So that would reuse. If you:
tag feature files and scenarios #firefox and #chrome
Create hooks to configure chrome vs firefox based on the #chrome and #firefox tags
then use separate test classes that filter to the #firefox and #chrome
make sure your junit runner is going to allow forking to run multiple tests at the same time
Then you should see your tests running in both containers at the same time.
This should be possible with the surefire plugin: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html

How to run an exported selenium test case using TestNG in Eclipse

I recorded a test case using Selenium IDE 1.10.0. I exported the case as Java/TestNG/Remote Control.
My Eclipse version is 4.2.0 and I installed TestNG plug-in version 6.8
I am wondering how can I create a project within the eclipse to run this exported test case?
Please give me some instructions or share with me some online tutorial / documentations. Thanks!
Below is the java code generated by the Eclipse:
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;
public class SearchDonor extends SeleneseTestNgHelper {
#Test public void testSearchDonor() throws Exception {
// set overall speed of the test case
selenium.setSpeed("4000");
selenium.open("/?html=openid");
selenium.click("css=input[type=\"submit\"]");
selenium.waitForPageToLoad("30000");
Thread.sleep(4000);
selenium.click("id=cmp_admin");
selenium.waitForPageToLoad("30000");
selenium.click("id=quicksearch_anchor");
selenium.click("css=img[alt=\"Member\"]");
selenium.waitForPageToLoad("30000");
selenium.type("id=search_name", "suzy");
selenium.click("css=input[type=\"image\"]");
selenium.click("link=Balagia, Suzy");
selenium.waitForPageToLoad("30000");
}
}
After some trial and errors. I managed to execute the exported code in Eclipse as TestNG Test. Here are the steps I took to make it compile.
1.created a Java Project within the Eclipse
2.add a class to the Java Project
2a. I copied and pasted the code into the newly created the class
3.Right click on the project>build path>configure build path
3a. on the Java Build Path window select libraries>Add External JARS, then adding all the JAR files from Selenium Java Driver folder (e.g. folder name in this case is libs), plus selenium-java-2.31.0.jar and selenium-java-2.31-src.jar
4.click the Run icon next the debug icon on the top menu bar, Run AS TestNG Test
My code is compiling, but I got some exception errors. I am getting stared on a TestNG test case!

Using Selenium 2.0 WebDriver in practice

I want to write Selenium test cases in JUnit and test my projects in multiple browsers and I would like to take advantage of the fact that all Selenium drivers implement the same interface.
Each test case should look like this:
package fm;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;
public class HomepageTest {
#Test
public void testTitle(WebDriver driver) {
driver.get("http://localhost/");
assertEquals("Foo", driver.getTitle());
}
#Test
public void testSearchForm(WebDriver driver) {
//...
}
}
The passed WebDriver implementations should be controlled somewhere centrally. I'll probably need to override some of the JUnit behaviour and I hope it's possible.
I want to do it this way in order to avoid two things:
Code repetition: If each test case would initialize all tested browsers in #Before, the test suite would have a lot of repeated code that is hard to maintain.
Speed of the test suite: If I had centralized control over the order and passed WebDriver implementations, I could easily manage to open for example Firefox, run all test cases in it, close it and open the next browser. If each test case would manage to open and close browsers on its own, it would add a lot of time to each test run.
Anybody have an idea how should I do it? Thanks.
In the Selenium project we inject what we need using http://code.google.com/p/selenium/source/browse/trunk/java/client/test/org/openqa/selenium/AbstractDriverTestCase.java and then our build calls the browser and we get tests running in it.
Have a look at our code base to get some inspiration!
Please check with ISFW it supports selenium webdriver/remote webdriver as well as conventional (selenium1) rc way.
You need to write code using regular selenium api
for example
selenium.open(url);
selenium.type("loc", "text to type");
selenium.submit("loc");
Here is the working demo. Set browser String as per your requirement.
The FW support selenium conventional way as well as selenium 2 webdriver. You need to set appropriate browser string in application properties. Following are different browser configurations for Firefox:
*firefox - required selenium server running on configured host/port
if not found then fw will check/start one on locahost/port
firefoxDriver – will run directly with firefox web driver without
selenium server
firefoxRemoteDriver - required selenium server running on
configured host/port if not found then fw will check/start one on
locahost/port, it will run test using firefox web driver on host
machine
Same way for IE - *iexplore, *iehta, iexplorerDriver, iexplorerRemoteDriver
and so on.
I did what you are/were? trying to do with a static class that controlls the webdriver and all my test which need the same webdriver get it from there. It really helps when you are running multiple tests that need to use the same session. And all your tests run in one browser, so not every tests opens a new browser instance.
Maybe you should also have a look at testNG. I made the experience that testNG is better for tests with selenium since it is not so focused on independent tests. It offers a lot of useful functionality.