Showing posts with label Selenium webdriver - code snippets. Show all posts
Showing posts with label Selenium webdriver - code snippets. Show all posts

Monday, June 13, 2016

Goals in Maven

The goal you indicate in the command line is linked to the lifecycle of Maven. For example, the buildlifecycle (you also have the clean and site lifecycles which are different) is composed of the following phases:
  • validate: validate the project is correct and all necessary information is available.
  • compile: compile the source code of the project.
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run.
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally.
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Sunday, February 16, 2014

Log4j implementation

Following are the steps to generate logs for the applications,

Step1: Create one sample project
Step2: Create Package in the project to place java classes
Step3: Download Lo4j jar file from http://logging.apache.org/log4j/1.2/download.html
Step4: Add log4j jar file to the project build path
Step5: Create log4j property file at the project level and add the properties into the file as shown in fig1.

#All==>all messages
#WARN==>only warning message  etc..
log4j.rootLogger=ALL

#log4j.logger.<Package>=,<log file name>FileAppender
log4j.logger.com.pack=,logTestFileAppender

# junit_log4jFileAppender - used to log messages in the junit_log4j.log file.
log4j.appender.logTestFileAppender=org.apache.log4j.FileAppender
log4j.appender.logTestFileAppender.File=logTest.log
log4j.appender.logTestFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.logTestFileAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %-4r [%t] %-5p %c %x - %m%n
 Fig 1: log.properties

Step6: Create one java class in the package
Step7: Create reference variable for logger class as shown in fig2.

public static Logger logger=Logger.getLogger(classname.class)
                Fig2: Reference Variable for Logger Class

Step8: In the main method load the logger properties by using configure() method in PropertyConfigurator Class.
Step9: By using Logger reference variable write info, debug and error etc types of logs as shown in fig3.

Logger.info(“START:: Starting of some XY()”);
Logger. debug(“Values passed to the some XY() are ::  a: ”+a);
 Logger. error(“ERROR:: Error occurred in some XY()”);
                Fig3: Sample code to write info, debug and error logs

Step10:  After completion of code run the project and find the generated logs in the path given in log.propeties for the property log4j.appender.logTestFileAppender.File”.

Example:

package com.pack;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LogExample {

    public static Logger logger=Logger.getLogger(LogExample.class);
   
    public void test() {
        WebDriver driver=new FirefoxDriver();
        logger.info("Browser Launched");
        driver.get("http://www.gmail.com");
        logger.info("URL Entered");
        logger.debug("Hi");
        logger.fatal("Hello");
    }
    public static void main(String[] args) {
        PropertyConfigurator.configure("log.properties");
LogExample example=new LogExample();
example.test();
    }

}
 

Thursday, August 15, 2013

Hnadling webtable using web driver



//Handling WebTable with verifications

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WebTable_SalesForce {
      
       WebDriver driver;
       @Test
       public void test() {
              driver=new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
              driver.get("http://www.salesforce.com");
              driver.findElement(By.id("button-login")).click();
              driver.findElement(By.id("username")).sendKeys("manu.immadi7373@gmail.com");
              driver.findElement(By.id("password")).sendKeys("selenium@123");
              driver.findElement(By.id("password")).sendKeys(Keys.ENTER);
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
              driver.findElement(By.xpath("//form[@id='hotlist']/table/tbody/tr/td[2]/input")).click();
              String firstName="Pavan";
              String lastName="Kalyan";
              String company="Tollywood";
              driver.findElement(By.id("name_firstlea2")).sendKeys(firstName);
              driver.findElement(By.id("name_lastlea2")).sendKeys(lastName);
              driver.findElement(By.id("lea3")).sendKeys(company);
              driver.findElement(By.xpath("//td[@id='bottomButtonRow']/input")).click();
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
             
              driver.findElement(By.xpath("//input[@name='go' and @value='Run Report']")).click();
              String xpath_Start="//*[@id='fchArea']/table/tbody/tr[";
              String xpath_End="]/td[1]";
              boolean recordfound=false;
              int i=2;
              while(isElementPresent(xpath_Start+i+xpath_End))
              {
                     String fname=driver.findElement(By.xpath(xpath_Start+i+xpath_End)).getText();
                     if(fname.equals(firstName)) {
                           //check the lastname
                           String lastnamecol=xpath_Start + i + xpath_End.replace("td[1]","td[2]");
                           String lname=driver.findElement(By.xpath(lastnamecol)).getText();
                           if(lname.equals(lastName)) {
                                  System.out.println("Both are same " +firstName + lastName);
                                  recordfound=true;
                                  i++;
                                  break;
                           }
                     }
              }
       Assert.assertTrue(recordfound, "Record NotAvilable");
              }
       public boolean isElementPresent(String objxpath) {
              int count = driver.findElements(By.xpath(objxpath)).size();
              if(count==0)
                     return false;
              else
                     return true;
       }
      
}

AI in Software Testing: How Artificial Intelligence Is Transforming QA

For years, software testing has lived under pressure: more features, faster releases, fewer bugs, smaller teams. Traditional QA has done her...