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.
- Now you would get the Environment variable Window,
- 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 |
- 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,
- 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.
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?
- 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.
- Once installed firebug, You would see the addon like this,
- 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/
- Click on the button ‘Add to Firefox’ and follow the steps to install firepath on firebug.
- Once installed firepath, You would get like this,
How does Authentication Works via Social Networks???
Most of you aware that we could login into most of the websites and social networking sites via google/ facebook/ twitter account login credentials, do you know how it’s working? Let’s see how.
OAuth is an open standard protocol for authorization. OAuth provides a method for clients to access server resources on behalf of a resource owner (such as a different client or an end-user). It also provides a process for end-users to authorize third-party access to their server resources without sharing their credentials (typically, a username and password pair), using user-agent redirections.
What is CAPTCHA?
Most of you have experienced with CAPTCHA test, let’s see the detail information about it.
What is CAPTCHA?
CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart) is known as a type of Human Interaction Proof (HIP).It is a type of challenge-response test used in computing to determine whether the user is human being or computer program.
You’ve probably seen CAPTCHA test on lot of Web sites. The most common form of CAPTCHA is an image of several distorted letters & numbers. It’s your job to type the correct series of letters into a form. If your letters match the ones in the distorted image, then you will pass the test.
For example, a free e-mail service might find itself attacked by account requests from an automated program. That automated program could be part of a larger attempt to send out spam mail to millions of people. The CAPTCHA test helps to identify which users are real human being and which ones are computer program.
Applications:
- Registration forms on Web sites often use CAPTCHAs. For example, free Web-based e-mail services like Hotmail, Yahoo! Mail or Gmail allow people to create an e-mail account free of charge.
- It is used to prevent people from using bots (Automated program) to assist with massive downloading of content from multimedia websites.
- It is used in online message boards and blog comments to prevent bots from posting spam links as a comment or message.
- CAPTCHAs are used to prevent bots from using various types of computing services, collecting certain types of sensitive information and preventing bots from taking part in online polls
How to use:
reCAPTCHA is a free CAPTCHA service. If you run a website that suffers from problems with spam, you can put reCAPTCHA on your site. For some applications (such as WordPress and Mediawiki), we have plugins that allow you to use reCAPTCHA without writing any code. We also have easy-to-use code for common web programming languages such as PHP.
References:
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.
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
- Eclipse
- JDK 1.6 or later
- Selenium web driver Jars (You could download from here http://selenium.googlecode.com/files/selenium-java-2.39.0.ziphttp://selenium.googlecode.com/files/selenium-java-2.39.0.zip)
Project Step-up
- Create a java project in Eclipse like below, File->New->Java Project
- Then create a Class named SeleniumMain (As your wish) like below,
- Now add the Selenium jar to build path, which is downloaded before.
- 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,
- While running the SeleniumMain.java class, You would get the output as below,
- 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/list
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>