Skip to main content

Posts

Showing posts from April, 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