Showing posts with label webdriver. Show all posts
Showing posts with label webdriver. Show all posts

Wednesday, 11 September 2013

Handling Captcha | Webdriver

Make use of the 'input' tag with type 'hidden' in-order to handle Captcha.

Look at this example for reference..


driver.get("http://www.google.com/recaptcha/learnmore");
driver.switchTo().frame(0); //calling iframe with no id
JavascriptExecutor js = (JavascriptExecutor) driver; 
//Setting the captcha values 
js.executeScript("document.getElementsByName('recaptcha_challenge_field')[0].setAttribute('value', '03AHJ_Vuv4tV3FrmUHbImL9JPkWJNqs1KDbFdKfG1jhqa2Uhl4U1vzLxXtZMMkZoAHuVCXA1js3GiaaQJ-zqyuledzZP-PEOV-y_Fx87-U6HVu4nh8kfwPzfPU50yEV5oscb20ptwMGR5EEoAtE8dfAlwCVejJtP779upzfAqn_ID5IQJ2F9Nw218')");
driver.findElement(By.name("recaptcha_response_field")).sendKeys("23129555894");
driver.findElement(By.name("Button1")).click();


Note: setAttribute plays a major role here.

Thursday, 3 January 2013

Selenium - Generate HTML Reports using JUnit+ANT


Apache Ant is an open source build tool. A build tool can be used to compile the source code, creating the build artifacts such as JAR, WAR, and EAR files.  Some of the other usage of ANT is to run unit tests, do the application deployment on containers such as JBoss, Tomcat, WebSphere, WebLogic, GlassFish, etc, and to run Automated Selenium Tests.

In this example, we will demonstrate how to Generate HTML Reports using ANT. Let's follow the given steps:
Step 1: Download Apache Ant
Download Apache Ant

OSArchive name
Windowsapache-ant-1.8.4-bin.zip
Linuxapache-ant-1.8.4-bin.tar.gz
Macapache-ant-1.8.4-bin.tar.gz
Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location where ANT libraries is stored on your machine. For example, We've stored Ant libraries in apache-ant-1.8.4 folder on various Operating Systems as follows.

OSOutput
WindowsSet the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
Linuxexport ANT_HOME=/usr/local/\apache-ant-1.8.4
Macexport ANT_HOME=/Library/\apache-ant-1.8.4
Append Ant compiler location to System Path is as follows for different OS:
OSOutput
WindowsAppend the string ;%ANT_HOME\bin to the end of the system variable, Path.
Linuxexport PATH=$PATH:$ANT_HOME/bin/
Macnot required
Step 3: Download Junit Archive

Download JUnit Archive
OSArchive name
Windowsjunit4.10.jar
Linuxjunit4.10.jar
Macjunit4.10.jar

Let us create a new project in Eclipse and write a build.xml file to execute the Java files in this project 

1) Create a new project – JavaProject
2) Create a new package (under src) – seleniumtest
3) Under this package, create new Java class – AntTest.java
4) Record a Selenium Test and export in JUnit 4 format as below

package seleniumtest;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

public class AntTest extends SeleneseTestCase{
private static SeleniumServer seleniumServer;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
        seleniumServer = new SeleniumServer();
        seleniumServer.start();
selenium.start();
}

@Test
public void testGoogleSearch() throws Exception {
selenium.open("/");
selenium.click("link=Advanced search");
selenium.waitForPageToLoad("30000");
selenium.type("as_q", "selftechy, selenium");
selenium.click("//input[@value='Advanced Search']");
selenium.waitForPageToLoad("30000");
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

}

Create ANT Build.xml under JavaProject

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="exec" basedir=".">

<property name="src" value="./src" />
<property name="lib" value="./lib" />
<property name="bin" value="./bin" />
<property name="report" value="./report" />
<path id="test.classpath">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>

<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
</target>

<target name="compile" depends="init">
<javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" includeantruntime="false">
<classpath>
<pathelement path="${bin}">
</pathelement>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>

<target name="exec" depends="compile">
<delete dir="${report}" />
<mkdir dir="${report}" />
<mkdir dir="${report}/xml" />
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</classpath>

<test name="seleniumtest.AntTest" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
<formatter type="xml" />
</test>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}/xml">
<include name="TEST*.xml" />
</fileset>
<report format="frames" todir="${report}/html" />
</junitreport>
</target>
</project>

Run the following ant command

C:\WORKSPACE\JavaProject\ant

Friday, 28 December 2012

"Failed to start: SocketListener0@0.0.0.0:4444" while Selenium RC Test Execution

Many of us face this common error while trying to execute tests after a successful test run. This is because of the Selenium server that starts on port 4444 and fail to shutdown, throwing an error message on forthcoming tests.

WARN - Failed to start: SocketListener0@0.0.0.0:4444


There are couple of alternate ways to overcome this

#1
1| Open a new tab from your web browser.
2| Copy and paste the below URL.
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
3| Soon-after hitting Enter button, a get response 'okok' will be obtained; it make sure that the Selenium server is shutdown on port 4444.








4| Now, its time to re-run your test on port 4444.

#2
1| Open cmd
2| Force Selenium server to listen port other other than 4444. i.e., 5555 or other
java -jar selenium-server-standalone-x.xx.x.jar -p 5555

Handling browser-level authentication | Selenium


Whenever a site prompts for an username and password, make use of the following script to override such authentication,

driver.get("http://username:password@site.com/page");

e.g., driver.get("http://admin:root@site.com/page");