Setting up Maven

What is Maven?

Maven is a “build & dependency management tool“. It addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies.

Maven Setup

Let’s see how to setup the maven.

Prerequisites

Download the maven from the below link,

http://www.gaidso.com/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.zip

Installation

  • Extract the apache-maven-3.1.1-bin.zip file and place the apache-maven-3.1.1 folder into C Drive or anywhere you want.

Environment Variable Setup

  • Go To control panel and search for environment then you would get System Icon. Then click on the Edit Environment variable for your account.

Environemnt Variable1

  • Now you would get the Environment variable Window,

Environemnt Variable2

  • Add the below Variables by click Add button,
Variable Name Variable Value
JAVA_HOME C:\Program Files\Java\jdk1.6.0_30
M2_HOME C:\apache-maven-3.1.1
PATH C:\ProgramFiles\Java\jdk1.6.0_30\bin; C:\apache-maven-3.1.1\bin

Environemnt Variable3

  • Now the Maven setup has been done, you could check whether the configuration is done properly or not by doing the below steps,
  • Goto Run and type cmd,

cmd

  • You would get command prompt as like below, then type mvn –version and press enter key. You could able to see the maven version details, if not probably you might be missed some configurations. So kindly verify the steps.

mvn

Happy Learning!!!

Selenium Basics

In the last post we have seen the steps to setup the project in eclipse. Today lets see the further more in selenium [Java] with writing a test case for ordering a product in ebay.

1. Create a class called EbayOrder.java

2. Load the driver based on your requirement either IE & Firefox, Here i am loading the Firefox driver.

// To load driver for firefox browser.
WebDriver driver = new FirefoxDriver();

3. Add implicit wait, to wait until the page loads or a particular milliseconds.

// Implicit wait for 10 seconds on each step to load the page.
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

4. To open a browser and goto ebay.co.in use driver.get() method,

// To navigate to the particular URL.
driver.get("http://www.ebay.in/");

5. To find the element based on the Xpath using firepath plugin and type the product name,

// To find the element by xpath and type something.
driver.findElement(By.xpath(".//*[@id='gh-ac']")).sendKeys("Sony xperia Z1");

6. To Click the search button using id, Which is submit button,

// To click the Search Button based on Id.
driver.findElement(By.id("gh-btn")).submit();

6. To wait until the particular element or page loads we could use the Explicit wait with conditions, Here i am using this for wait until the product list load,

// To wait until the product list to be clickable.
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")));

7. To select the product based on xpath,

// To Select the Product.
driver.findElement(By.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")).click();

8. To Click the BuyNow button,

// To Click BuyNow.
driver.findElement(By.xpath(".//*[@id='but_v4-28binLnk']")).click();

9. Like above You Could continue the test case like login, payment and so on.,

10. To exit at the end,

driver.close();

EbayOrder.java

package com.mahesh.selenium.Main;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class EbayOrder {

public static void main(String[] args) {
 // To load driver for firefox browser.
 WebDriver driver = new FirefoxDriver();

// Implicit wait for 10 seconds on each step to load the page.
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

// To navigate to the particular URL.
 driver.get("http://www.ebay.in/");

// To find the element by xpath and type something.
 driver.findElement(By.xpath(".//*[@id='gh-ac']")).sendKeys(
 "Sony xperia Z1");

// To click the Search Button based on Id.
 driver.findElement(By.id("gh-btn")).submit();

// To wait until the product list to be clickable.
 //WebDriverWait wait = new WebDriverWait(driver, 100);
 //wait.until(ExpectedConditions.elementToBeClickable(By
 //.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")));

// To Select the Product.
 driver.findElement(
 By.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img"))
 .click();

// To BuyNow.
 driver.findElement(By.xpath(".//*[@id='but_v4-28binLnk']")).click();

// Like above You Could continue the test case like login & payment.

//driver.close();
 }

}

Happy Learning!!!

How to do Pattern based Search in String and replace with another in Java

In our day today life we have faced some challenges like, to replace the particular pattern (String) by some other string. In this scenario we could use the Pattern matcher with regular expressions. Let’s see how we could achieve it.

Our input string is,

String inputStr = "Every week here and in our newsletter, 143-37-8945 we feature a new developer/blogger from the DZone community to catch up and find out what243-45-3442 he or she is working";

In this above String I want to find the numbers like this 143-37-8945 & 243-45-3442 and mask the last 4 digits.

Now we have to form a regular expression like as below,

(\\d{3}-\\d{2}-\\d{4})

Then we have to match the pattern with input string and find the pattern XXX-XX-XXXX, then have to mask it.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternSearch {
public static void main(String[] args) {

// Input string.
String inputStr = "Every week here and in our newsletter, 143-37-8945 we feature a new developer/blogger from the DZone community to catch up and find out what243-45-3442 he or she is working";

// Regular expression to find SSN.
Pattern pattern = Pattern.compile("(\\d{3}-\\d{2}-\\d{4})");

Matcher matcher = pattern.matcher(inputStr);

StringBuffer result = new StringBuffer();

// Every match of SSN, this loop will execute.
while (matcher.find()) {
System.out.println(matcher.group(1));
matcher.appendReplacement(result, matcher.group(1).substring(0, 7)
+ "XXXX");
}
matcher.appendTail(result);
System.out.println(result);
}
}

Output:

143-37-8945

243-45-3442

Every week here and in our newsletter, 143-37-XXXX we feature a new developer/blogger from the DZone community to catch up and find out what243-45-XXXX he or she is working.

Happy Learning!!!

What is firebug & fire path?

Firebug is a web development tool that facilitates the debugging, editing, and monitoring of any website’s CSS, HTML, DOM, XHR, and JavaScript;

FirePath is a Firebug extension that adds a development tool to edit, inspect and generate XPath expressions.

Using this firebug & firepath we could able to find the html element attributes.

How to install Firebug & Firepath?

  1. Goto https://getfirebug.com/ and click on the installfirebug button and follow the steps and install firebug as an add-on in the Mozilla Firefox browser.
  2. Once installed firebug, You would see the addon like this,firebug1firebug2
  1. After you have installed firebug you could be able to install firepath, go to the url: https://addons.mozilla.org/en-US/firefox/addon/firepath/
  2. Click on the button ‘Add to Firefox’ and follow the steps to install firepath on firebug.
  3. Once installed firepath, You would get like this,firepath

What is QR Code?

You’ve probably seen Quick Response (QR) codes like squares with squiggly lines and designs at random places around town. For ex. in coke bottle.
The QR Code system has become popular due to its fast readability and greater storage capacity compared to standard UPC barcodes.

QR CodeQR Details

It serves variety of function: Scan them with your smart phone, and they’ll take you to a Web site or phone app, give you coupons and maps to find restaurants or businesses or help you remember to have your car serviced. You can even imprint one on a T-shirt to take the place of business cards. QR codes help draw you to all sorts of places and events.

Lot of free websites available to generate the QR code such as http://www.qrcode-monkey.com/.

Selenium Web Driver [Java] Introduction

What is Selenium?

Selenium is an automate testing framework for web applications. Selenium is composed of multiple software tools. Each has a specific role. Here let’s see about selenium web driver.

Selenium Web Driver

Selenium Web Driver accepts commands and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser, and retrieves results. Let’s see how to set up and do a simple automate test using java language.

Prerequisites

Project Step-up

  1. Create a java project in Eclipse like below, File->New->Java ProjectCreating Java Project
  2. Then create a Class named SeleniumMain (As your wish) like below,Create Class
  3. Now add the Selenium jar to build path, which is downloaded before.

    Adding Jars into built path

  4. Now you could start write the code for testing the Application, Here let’s see how to open a flipkart website in Mozila firefox browser,Firefox Driver
  5. While running the SeleniumMain.java class, You would get the output as below,Firefox O/P
  6. If u want to use Internet explorer, the you should load the IEDriverServer.exe, which available in the below path,http://code.google.com/p/selenium/downloads/listIE Driver
    SeleniumMain.Java

    <br>package com.mahesh.selenium.Main;import java.io.File;  <p>import org.openqa.selenium.WebDriver;<br>import org.openqa.selenium.ie.InternetExplorerDriver;</p> <p>public class SeleniumMain {</p> <p>/**<br>* @param args<br>*/<br>public static void main(String[] args) {</p> <p>// Firfox driver.<br>//WebDriver driver = new FirefoxDriver();</p> <p>//IE Driver<br>File file = new File("C:/Jars/IEDriverServer.exe");<br>System.setProperty("webdriver.ie.driver", file.getAbsolutePath());<br>WebDriver driver = new InternetExplorerDriver();</p> <p>driver.get("http://www.flipkart.com");<br>}<br>}<br>

    IE ResultHappy Learning!!!