Tuesday, 14 April 2015

Compare String with/without using equals() method.

import java.util.Scanner;

public class CompareString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the 1st String: ");
        String s1 = in.next();
        System.out.println("Enter the 2nd String: ");
        String s2 = in.next();

        //1st approach without using equals() method
        System.out.println("*********compare by 1st approach************");
        if(s1.length()==s2.length()){
            for(int i=0; i<s1.length(); i++){
                if(s1.charAt(i)!=s2.charAt(i)){
                    System.out.println("String "+s1+" is not equal to string "+s2);
                    break;
                }
            }
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }

        //2nd approach , just use equals() method
        System.out.println("*********compare by 2nd approach************");
        if(s1.equals(s2)){
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }    
    }
}

output- 

Enter the 1st String: 
selenium
Enter the 2nd String: 
selenium
*********compare by 1st approach************
String selenium is equal to string selenium
*********compare by 2nd approach************
String selenium is equal to string selenium

Real time scenario to check item added to cart or not

Scenario-

1. Browse and open http://www.infibeam.com as a input data from excel sheet...
2. Navigate to all stores then mobiles and select mobiles type...
3. Then enter blackberry in search box and click on search...
4. select price range 10000 to 18000...
5. select displayed mobile, and write model name and price in a Textfile....
and check the product Instock or not,if it is Instock then click on Buy Now...(write Reusable script)
6. check it is added in to the cart or not...
7. Close the Browser.

import java.io.BufferedWriter;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Siva {
public static void main(String[] args) throws InvalidFormatException, IOException{
String url = getData(0,0);
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(url);
WebElement mob = driver.findElement(By.xpath("//span[text()='Mobiles & Tablets']"));
Actions act = new Actions(driver);
act.moveToElement(mob).perform();
WebElement type = driver.findElement(By.linkText("Mobile Type"));
act.moveToElement(type).click().perform();
driver.findElement(By.id("suggest")).sendKeys("blackberry");
driver.findElement(By.xpath("//form[@id='srchForm']//input[@value='Search']")).click();
driver.findElement(By.xpath("//a[@title='10000-18000']")).click();
List<WebElement> model = driver.findElements(By.xpath("//span[@class='title']"));
List<WebElement> price = driver.findElements(By.xpath("//div[@class='price']"));
FileWriter fileWriter = new FileWriter("out.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("model "+"\t\t\t\t\t"+"price"+"\n");

for(int i=0; i<model.size(); i++){
bufferedWriter.write(model.get(i).getText()+"\t"+price.get(i).getText()+"\n");
}
bufferedWriter.close();

model.get(0).click();
driver.findElement(By.xpath("//input[@value='Buy Now']")).click();
String quantity = driver.findElement(By.xpath("//div[@class='quantity']/div")).getText();
int n = Integer.parseInt(quantity);
if(n==1){
System.out.println("phone has been added to cart");

driver.close();
}
public static String getData(int row, int cell) throws InvalidFormatException, IOException{
FileInputStream fis = new FileInputStream("./InputData.xlsx");
Workbook wb = WorkbookFactory.create(fis);
String value = wb.getSheet("Sheet3").getRow(row).getCell(cell).getStringCellValue();
return value;
}
}

How to sort and remove the duplicates from array without using sort() method.

import org.apache.commons.lang3.ArrayUtils;

public class SortAndRemoveDuplicate {
    public static void main(String[] args) {
        int[] a = {1,5,3,2,1,11,7,5};
        int l = a.length, s=0;        
        for(int j=0; j<l; j++){
            for(int i=0; i<l-1; i++){
                if(a[i]>a[i+1]){
                    s = a[i];
                    a[i] = a[i+1];
                    a[i+1] = s;
                }else if(a[i]==a[i+1]){
                    a = ArrayUtils.remove(a, i);
                    l = a.length;
                }
            }
        }
        for(int n: a){
            System.out.println(n);
        }
    }
}

How to Generate XSLT report ?

Its a kind of user friendly report. XSLT stands for XML (Extensible Markup Language) Stylesheet Language for Transformations.  XSLT gives interactive (user friendly) reports with "Pie Chart".

Steps to generate XSLT Report-

1) 1st execute the test cases using ANT tool. Here are the steps to setup and execute the framework through ANT tool - click here 

2) Download the XSLT. Please follow the below steps to download.

i) click on this button shown below.



ii) Then click on the download button.



3) After downloading, unzip the downloaded folder-

After unzipped, you will get,



4) Now from the above, copy lib and testng-results.xsl inside your project directory. Don't copy build.xml because you have created that already while doing ANT setup.
Here we are referring the same project which we have already done using POM- LinkedIN_Project_By_POM



5) After copying above files, add the jars files to your project through build path from the lib folder which you have copied in the above step.



6) Now we are ready to gen the XSLT report. As we have already executed our test cases through ANT so now,
i) open cmd and navigate to the project directory.



ii) Then just type ant generateReport and hit enter.



iii) Now a folder will be generated with the name testng-xslt



iv) Open this report in browser-



This is the xslt report-


Note- If you not able to see this kind of report in your eclipse then just copy the url path and open that url in mozilla, it will open in the above format.