Tuesday 8 January 2013

Calling a java class as ant target

Here is the way to do it

classname: is filename of java :Ex: AutomationFileSequence
 arg: Any arguments if required to run the class needs to be mentioned here.

<target name="run_PostData" >
  <java fork="false" failonerror="yes" classname="com.qa.automation.main.AutomationFileSequence" classpathref="classpath"> 
            <arg line="${basedir}/Automation_PostData.properties"/>
    <arg line="PostData"/>
            </java>
            </target>

Sample Ant Script for running targets parallel and sequential

Recently ,I faced a challenge for making one target which can run the entire suite with lot of module dependencies.This was resolved with the help of ant targets: sequential and parallel.


Requirement :
I need to run the following sequetially

1.compile target
2. run and run_psdata targets paralelly
3.run_PreData,run_DFPPulls,run_AdTechPulls.... parallelly
4.run_PostData,run_Sales parallely
5.run_Administration,run_E2EPartner360,..... parallelly
6.run_Alerts,run_CustomFields  parallelly
7.Stop selenium

Below is the code to do all the above steps sequentially


<target name="CItarget">    
        <sequential>
            <antcall target="compile"/>
            <parallel>
              <antcall target="run"/>
              <antcall target="run_PSDATA"/>
            </parallel>
             <parallel>
                <antcall target="run_PreData"/>
                <antcall target="run_DFPPulls"/>
                <antcall target="run_AdTechPulls"/>
                <antcall target="run_AppnexusPulls"/>
                <antcall target="run_FTPPulls"/>
                <antcall target="run_OASPulls"/>
                <antcall target="run_GDFPPulls"/>
                <antcall target="run_FreewheelPulls"/>
                <antcall target="run_ThirdPartyPulls"/>
            </parallel>
    <parallel>
        <antcall target="run_PostData"/>
        <antcall target="run_Sales"/>
    </parallel>
            <parallel>
                <antcall target="run_Administration"/>
                <antcall target="run_E2EPartner360"/>
                <antcall target="run_Finance"/>
                <antcall target="run_Loaders"/>
                <antcall target="run_Accounts"/>
                <antcall target="run_Adops"/>
            </parallel>
            <parallel>
                 <antcall target="run_Alerts"/>
                <antcall target="run_CustomFields"/>
            </parallel>
            <antcall target="stop-selenium"/>
       </sequential>
    </target>

Interview Questions for Automation Engineers

1.Access Modifiers of JAVA
2.final finaly and finalize
3.real example for abstract and interface
4.Launch firefox driver using webdriver. (driver driver = new firefox driver();)selenium selenium = new default selenium(driver,siteurl.com);
5.hw 2 handle file upload using webdriver
6.which is the plugin in ie to find d locator(developer tool)
7.diff btwn sel and seln 2
8.what is hashmap
9.Explain OOPS concept
10.TestNG framework.(How to pass parameters to the test method)
11. why static keyword is used in main declaration

Suite creation in Junit4

Suite creation in Junit4

First Test



package com;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class FirstOne {
    
    private static SeleniumServer seleniumServer;
    private static Selenium selenium = new DefaultSelenium( "localhost",4444, "*chrome","http://www.google.com");
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        seleniumServer = new SeleniumServer();
        seleniumServer.start();
        selenium.start();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
          selenium.stop();
          seleniumServer.stop();
    }

    @Test
    public void Testing() throws Exception{
        selenium.open("/");
        selenium.waitForPageToLoad("6000");
        selenium.type("q", "search");
        selenium.click("btnG");
 }

}

Creating Test Suite in TestNG

Create a Click class with 2 methods testClick1,testClick2 and Suite called ClickSuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">


import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class Click {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.co.in/");
seleniumserver.start();
selenium.start();
}

@Test
public void testClick()throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.click("link=regexp:Advanced [s|S]earch");
selenium.waitForPageToLoad("5000");
selenium.click("id=opt-icon");
System.out.println(selenium.getLocation());
selenium.click("link=Scholar");
Thread.sleep(10000);
}


@Test
public void testClick2() throws Exception {
selenium.open("/");
selenium.type("q", "selenium link locator");
selenium.click("btnG");
}

@AfterClass
public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();


}
}





Now make ClickTestSuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ClickTestSuite" verbose="3">

<test name="testClick">
<classes>
<class name="Click"></class>
</classes>
</test>
<test name="testClick2">
<classes>
<class name="Click"></class>
</classes>
</test>

</suite>

Run selenium tests in IE 8 (windows 7)

In windows 7, it is not possible to run selenium tests in IE 8. However, there is a workaround. Go to IE 8, Tools->Compatibility view Settings and then check the Display all sites in compatibility view. Now if you type *iexplore in your code, it will run perfectly in the browser.

Code to start and stop Selenium server with eclipse setup

This is the minimum code to start the server and shutdown.


package file;

import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.SeleniumServer;

public class Reset extends SeleneseTestCase {

   private SeleniumServer seleniumServer;
   private Selenium selenium = new DefaultSelenium( "localhost",4444,
            "*chrome","http://");

  public void setUp() throws Exception
      {
          seleniumServer = new SeleniumServer();
          seleniumServer.start();
          selenium.start();
      }


//Test methods shd go here

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

}
Eclipse Configuration

create new project->enter project name->next->libraries->add selenium server.jar and junit-4.8.2.jar and click finish.

Way to export the java program to eclipse IDE.

1)After recording test case,click on options->format->Junit 3
2)Copy the method and paste it in between setup and teardown method.
3)Run->run as Junit

You can see your test case running...Enjoy!!!

Creating test suites with Junit4

Creating test suites with Junit4

Test Suite can be created in JUnit 4 as

import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
//nothing
}

The @RunWith(Suite.class) gives opportunity to combine both JUnit 4 and JUnit 3 tests and test cases together:


@RunWith(Suite.class)
@Suite.SuiteClasses({
 ExampleOfJunit3TestSuite.class,
 ExampleOfJUnit3TestCase.class,
 ExampleOfJUnit4TestSuite.class,
 ExampleOfJUnit4TestCase.class})
public class BothJUnit4and3TestSuite {
}

Locating Elements with Same name


These are the buttons with same name





Here goes the links with same name

Link

Link


Program to locate this are

package file;

import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Twonames extends SeleneseTestCase {

private SeleniumServer seleniumServer;
private Selenium selenium = new DefaultSelenium( "localhost",4444,
       "*chrome",
       "http://localhost:8080/same.html");

public void setUp() throws Exception
{
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

public void SameName() throws Exception {
selenium.open("/same.html");
selenium.setSpeed("2000");
selenium.click("//input[@id='button'and position()=2 ]");
  System.out.println("clicked 2nd button");
selenium.click("//input[@id='button'and position()=1 ]");System.out.println("clicked 1st Button");
   selenium.click("//a[@name='search' and position()=2]");System.out.println("clicked 2nd Link");
selenium.isTextPresent("HI");
}


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

}

Selenium Overview:-

Selenium Overview:-

Selenium is a suite of tools to automate web app testing across many platforms. It is a GUI based automation tool. Initially it is built by ThoughtWorks. It supports various browsers on various platforms
Selenium Projects
Selenium has many projects. Following projects are mostly used by testers.
  1. Selenium IDE (IDE)
    Selenium IDE can be used only in FireFox. It is an add-on for FireFox. User can record the actions and can edit and debug the tests. It can be used to identify IDs, name and XPath of objects. Only one test at a time.
  2. Selenium Core (CORE)
    Selenium Core is the original Javascript-based testing system. This technique should work with any JavaScript enabled browser. It is the engine of both, Selenium IDE and Selenium RC (driven mode), but it also can be deployed on the desired application server. It is used specifically for the acceptance testing.
    User can record the tests using Selenium IDE and can use the same tests to run in other browsers with minimal modifications. It provides support to run the tests in HTA (HTML Applications) Mode. This mode works only in IE.
  3. Selenium Remote Control (RC)
    Selenium Remote Control is a test tool that allows user to write automated web application UI tests in few programming languages against any HTTP website using any mainstream JavaScript-enabled browser. User can write the tests (More expressive programming language than the Selenese HTML table format) in Java, DotNet, Perl, Ruby and PHP. Also it supports few testing frameworks.
  4. Selenium Grid
    Selenium Grid allows easily to run multiple tests in parallel, on multiple machines, in an
    heterogeneous environment by cutting down the time required for test execution. Using this, user can run multiple instances of Selenium Remote Control in parallel.
Supported browsers
Selenium tools can run in following browsers.
  • Internet Explorer
  • FireFox
  • Opera
  • Safari
  • Seamonkey
Supported Operating Systems
Users can execute the selenium tests in following OS.
  • Windows
  • Linux
  • Solaris
  • OS X
Supported Programming languages
Below languages are supported by Selenium RC.
  • C# (DotNet)
  • Java
  • Perl
  • Ruby
  • Python
  • PHP
Sample Code
Following program tells how to launch a browser and to play around with text,list,combobox objects we find normally in any website
package Orkut;
import com.thoughtworks.selenium.*;
import com.thoughtworks.selenium.DefaultSelenium;
public class MySelenium {
static Selenium selenium;
static boolean val,val1;
public static void main(String arg[]) throws Exception
{
selenium = new DefaultSelenium("localhost",
4444, "*iexplore", "http://www.ge.com");
selenium.start();
Thread.sleep(4000);
selenium.windowMaximize();
selenium.windowFocus();
selenium.open("/index.html");
selenium.click("//div[@id='ge_footer']/ul/li[2]/a");
val=selenium.isElementPresent("//img[@alt='GE: imagination at work']");
for (int second = 0;second<=10; second++) {
val=selenium.isElementPresent("//img[@alt='GE: imagination at work']");
if (val)
{
System.out.println(val);
break;
}
else
{
if (second==10)
{
System.out.println("Page not found :=GE: imagination at work");
selenium.stop();
}
Thread.sleep(4000);
}
}
selenium.click("//ul[@id='ge_secondaryNav']/li[2]/a");
val1=selenium.isElementPresent("//div[@id='secondary_content']/div/h3");
for (int second = 0;second<=10; second++) {
val1=selenium.isElementPresent("//div[@id='secondary_content']/div/h3");
if (val1)
{
System.out.println(val1);
break;
}
else
{
if (second==10)
{
System.out.println("Element not found :=GE: Other Contact Resources");
selenium.stop();
}
Thread.sleep(4000);
}
}
selenium.click("//input[@id='contact_type_press']");
selenium.select("//select[@id='contact_subject']", "label=Other");
selenium.select("//select[@id='contact_country']", "label=Algeria");
selenium.type("//input[@id='contact_email']", "abc@vvv.com");
selenium.type("//textarea[@id='contact_comments']", "testing");
//selenium.click("//form[@id='contact_form']/p/input");
}
}