Skip to main content

Posts

Showing posts from 2014

Invoke testng.xml from a batch file with code

Executing tests from a .bat file is familiar or useful while using GRID. There are couple of ways to execute this. 1| Copy any one of the below methods in text file 2| Edit the drive location 3| Save it as yourtext .bat 4| Now, double click on the batch file created. Note: Its better to execute from cmd prompt before try Method #1 cd   C:\ Workspace\projectname java -cp C:\Workspace\projectname\lib\*;C:\Workspace\projectname\bin org.testng.TestNG testng.xml Method #2 cd C:\ Workspace\projectname set ProjectPath=C:\Workspace\projectname echo %ProjectPath% set classpath=%ProjectPath%\bin;%ProjectPath%\lib\* echo %classpath% java org.testng.TestNG %ProjectPath%\testng.xml

Database connection and selenium with examples

This illustrates how to connect MySQL database with Selenium using Java bindings.  Make sure mysql-connector-java-5.x.x-bin.jar in your buildpath. Configure MySQL 1| Download an install  MySQL . 2| Create a database and table. e.g., create database one; create table pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); 3| Follow up whatever queries intended 4| Now, try to connect the schema with java via selenium webdriver for retrieving the table values. private String dbvalue; private String dbUrl = "jdbc:mysql://localhost: 3306 / one "; //Here, one is the schema/db private String username="root"; private String password=" yourpassword "; private String dbClass = "com.mysql.jdbc.Driver"; @Test public void connectdb() throws Exception {    driver.get("www.xyz.com");        try { Class.forName(dbClass); Connection con = DriverManager.getConnection (dbUrl,user

Zoom in Zoom out actions in selenium

Zoom in WebElement html = driver.findElement(By.tagName("html")); html.sendKeys(Keys.chord(Keys.CONTROL, Keys. ADD )); Zoom out WebElement html = driver.findElement(By.tagName("html")); html.sendKeys(Keys.chord(Keys.CONTROL, Keys. SUBTRACT )); Zoom 100% WebElement html = driver.findElement(By.tagName("html")); html.sendKeys(Keys.chord(Keys.CONTROL,  "0" )); Zoom Feature on MAC WebElement html = driver.findElement(By.tagName("html")); html.sendKeys(Keys.chord(Keys. COMMAND , Keys.ADD)); html.sendKeys(Keys.chord(Keys. COMMAND , Keys.SUBTRACT)); html.sendKeys(Keys.chord(Keys. COMMAND , Keys."0"));

Different locators in selenium with examples

Types of Element Locators : 1| Html id | id attribute 2| Html name | name attribute 3| XPATH 4| Class name 5| CSS Selector 6| Link Text 7| Tag Name XPATH XPath is a way to navigate in xml document and this can be used to identify elements in a wen page. You may have to use XPath when there is no name/id associated with element on page or only partial part of name/IDE is constant. Direct child - / Relative child - // id, class, names can also be used with XPath //input[@name='q'] //input[@id='q'] //input[@class='q'] e.g.,  By.xpath(“//input[@id=’myElementId’]”) CSS Selector CSS location strategy can be used with Selenium to locate elements, it works using cascade style sheet location methods.in which - Direct child is denoted with - (a space) Relative child  is denoted with - > id, class, names can also be used with CSS css=input[name='q'] css=input[id='q'] or input#q css=input[class='q'] or input.q

Page scroll in selenium with examples

Scroll Down: import org.openqa.selenium.JavascriptExecutor; WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("scroll(0, 250 )"); //y value '250' can be altered Scroll up: JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("scroll( 250 , 0)"); //x value '250' can be altered Scroll bottom of the Page: JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));"); (or) Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); Full scroll to bottom in slow motion: for (int second = 0;; second++) {         if(second >=60){             break;         }             ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, 400 )", "&qu

Implicit and Explicit Waits in selenium with examples

Explicit Wait is related with certain conditions to wait; Implicit Wait with specific time to wait for an Element. Explicit Wait Here, the driver waits for 10 secs till the web element is found;  If not, it simply throws a Timeout Exception. WebElement element = (new WebDriverWait(driver, 10 ))   .until(ExpectedConditions. presenceOfElementLocated (By.id(" Value "))); or| WebDriverWait wait = new WebDriverWait(driver, 20 ); WebElement element = wait.until(ExpectedConditions. elementToBeClickable (By.id(" Value "))); WebDriverWait can't be declared globally and throw NullPointerException; whereas the below snippets will help you to do so. // Handles any locator @Test public void Test01() throws Exception {   driver.get(" www.xyz.com ");   wait().until(ExpectedConditions. presenceOfElementLocated (By.xpath(" Value ")));   } private WebDriverWait wait()    {     return new WebDriverWait(driver,  20 );    } // Handl

Page Object in selenium with example

Page Object Pattern is a good approach of implementing Automation tests. It is a language neutral pattern for representing a complete page or portion of a page in an Object Oriented manner. Pattern is a page object, which encapsulates the behavior of the page in an Object oriented manner. It need some programming skills too. Why Page Object? 1| Maintenance 2| Readability of scripts 3| Separation of Concerns Page Object Pattern | Page Factory Lets start with an example using Google.com SCENARIO  1| Open URL 2| Search google 3| Assert page title TEST CLASS | TC.java package  packagename ; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TC {  private WebDriver driver;  public ASSERT Task;    @BeforeTest  public void setUp() throw

Handle AJAX elements in Selenium 2 (WebDriver)

One approach is to use FluentWait and a Predicate available with Selenium2. The advantage of this approach is that element polling mechanism is configurable. The code example below waits for 1 second and polls for a textarea every 100 milliseconds. FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA")); fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS); fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS); fluentWait.until(new Predicate<By>() { public boolean apply(By by) { try { return browser.findElement(by).isDisplayed(); } catch (NoSuchElementException ex) { return false; } } }); browser.findElement(By.tagName("TEXTAREA")).sendKeys("text to enter");       Another approach is to use ExpectedCondition and WebDriverWait strategy. The code below waits for 20 s

RFT: Example for ITestDataTable, It helps how to access web table using RFT

import resources.parse_table_idHelper; import com.rational.test.ft.*; import com.rational.test.ft.object.interfaces.*; import com.rational.test.ft.object.interfaces.SAP.*; import com.rational.test.ft.object.interfaces.siebel.*; import com.rational.test.ft.script.*; import com.rational.test.ft.value.*; import com.rational.test.ft.vp.*; public class parse_table_id extends parse_table_idHelper { public void testMain(Object[] args) { try { //Search for html table TestObject[] foundTables = RationalTestScript.find( atDescendant(".class", "Html.TABLE", ".id", "my_table")); if (foundTables.length != 1) throw new Exception("Table not found"); StatelessGuiSubitemTestObject html_table = (StatelessGuiSubitemTestObject)foundTables[0]; // table parsing ITestDataTable table = (ITestDataTable)html_table.getTestData("contents"); int nbRows = table.getRowCount(); int nbCols = table.getColumnCount(); for (int i=0; i

How to install Gride 2.0 ?

In this section, you will use 2 machines. The first machine will be the system that will run the hub, while the other machine will run a node. For simplicity, let us call the machine where the hub runs as “Machine A” while the machine where the node runs will be “Machine B”. It is also important to note their IP addresses. Let us say that Machine A has an IP address of 192.168.1.3 while Machine B has an IP of 192.168.1.4. Step 1 Download the Selenium Server by   here .   Step 2 You can place the Selenium Server .jar file anywhere in your HardDrive.But for the purpose of this tutorial, place it on the C drive of both Machine A and Machine B. After doing this, you are now done installing Selenium Grid. The following steps will launch the hub and the node. Step 3 ·          * We are now going to launch a hub. Go to Machine A. Using the command prompt, navigate to the root of Machine A’s - C drive ,because that is the directory where we placed the Selenium Server.