Automation Using Selenium Webdriver

Friday, 21 October 2016

Windows Handling in selenium webdriver


Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows.

package com.windowshandling;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Windows_Handle {
public static WebDriver d;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
d=new ChromeDriver();
d.navigate().to("https://www.facebook.com");
d.manage().window().maximize();
String mainwindow=d.getWindowHandle();
System.out.println("unique id for mainwindow:::"+mainwindow);
d.findElement(By.linkText("Terms")).click();
Set<String>windows=d.getWindowHandles();

for(String eachwindow:windows){
System.out.println("window id is:::"+windows);
if(!mainwindow.equals(eachwindow))
{
d.switchTo().window(eachwindow);
System.out.println("subwindow title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();

d.switchTo().window(mainwindow);
System.out.println("title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();
String text=d.findElement(By.xpath(".//*[@id='globalContainer']/div[3]/div/div/div/div[2]/a")).getText();
System.out.println("message is::"+text);
      }
       }
    }
}

Compare Strings Examples in java


package com.methods;
public class String_Functions {
public static void main(String[] args) {
String Str1="iam learning selenium";
String str2="   iam lazy to practise";
//equals function
System.out.println("comapring two styrings::"+Str1.equals(str2));
//concat function
System.out.println("Concatenation two strings:::"+Str1.concat(str2));
//charAt function
String st1="iam so beautifull";
System.out.println("index of charaAT:::"+st1.charAt(5));
//length function
System.out.println("lenght of st1::"+st1.length());
//toLowerCase function
String s="sELeNIuM";
System.out.println("LOWERCASE::"+s.toLowerCase());
//toUpperCase function
String s1="SEleIuM";
System.out.println("LOWERCASE::"+s1.toUpperCase());
//indexOf function
String t="iam very happy";
System.out.println("index of:::"+t.indexOf("very"));
//valueOf function
int j=100;
System.out.println("convert int to string::"+String.valueOf(j));
//parseInt function
String i="200";
System.out.println("convert string to int::"+Integer.parseInt(i));
//substring function
String st="i want job without practise";
System.out.println("substring is:::"+st.substring(11, 18));
//substring function
String p="RS/-300/-";
System.out.println("price substring is:::"+p.substring(4,7));
//split function
String splt="HCL@INFOSYS@DELOITE@IBM@GOOGLE@TECH";
String arrayvar[]=splt.split("@");
for(String each:arrayvar){
System.out.println("each index::"+each);
}
//split function
String sn="i want job in top mnc but i will not be regualr to class";
String var[]=sn.split(" ");
for(String eachindex:var){
System.out.println("index is::"+eachindex);
}
//trim function
String tr="                    testing tools                ";
System.out.println("trim vallue is::"+tr.trim());

}

}

Comparing Strings:
In order to compare Strings for equality, you should use the String object's equals or equalsIgnoreCase
 methods.

For example, the following snippet will determine if the two instances of String are equal on all
 characters
String firstString = "Test123";
String secondString = "Test" + 123;
if (firstString.equals(secondString)) {
   // Both Strings have the same content.
}
This example will compare them, independent of their case


import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
            public static void main (String[] args) throws java.lang.Exception
            {
                        String firstString = "Test123";
                        String secondString = "TEST123";
                        if (firstString.equalsIgnoreCase(secondString)) {
                           System.out.print("Equal");
                        }
            }
}

Comparing Strings in switch statement

String stringToSwitch = "A";
switch (stringToSwitch) {
    case "a":
        System.out.println("a");
        break;
    case "A":
        System.out.println("A"); //the code goes here
        break;
    case "B":
        System.out.println("B");
        break;
    default:
        break;
}

Changing the case of characters within a String


The String type provides two methods for converting strings between upper case and lower case:

toUpperCase to converts all characters to upper case
toLowerCase to convert all characters to lower case
These methods both return the converted strings as new String instances:
the original String objects are not modified.
String string = "This is a Random String";
String upper = string.toUpperCase();
String lower = string.toLowerCase();

System.out.println(string);   // prints "This is a Random String"
System.out.println(lower);    // prints "this is a random string"
System.out.println(upper);    // prints "THIS IS A RANDOM STRING"


Finding a String Within Another String

To check whether a particular String a is being contained in a String b or not, we can use the method
 String.contains() with the following syntax:

b.contains(a); // Return true if a is contained in b, false otherwise
The String.contains() method can be used to verify if a CharSequence can be found in the String.
The method looks for the String a in the String b in a case-sensitive way.

String str1 = "Hello World";
String str2 = "Hello";
String str3 = "helLO";

System.out.println(str1.contains(str2)); //prints true
System.out.println(str1.contains(str3)); //prints false


Reversing Strings

There are a couple ways you can reverse a string to make it backwards.

StringBuilder/StringBuffer:

String code = "code";
 System.out.println(code);

 StringBuilder sb = new StringBuilder(code);
 code = sb.reverse().toString();
Char array:
String code = "code";
System.out.println(code);

char[] array = code.toCharArray();
for (int index = 0, mirroredIndex = array.length - 1; index < mirroredIndex; index++, mirroredIndex--) {
    char temp = array[index];
    array[index] = array[mirroredIndex];
    array[mirroredIndex] = temp;
}

// print reversed
System.out.println(new String(array));

 System.out.println(code);

value labs testing interview questions 8th oct

value labs testing interview questions
1st technical
From Java
1.What is the Difference between final,finally,finalize
2.what is the difference between Call by value and call by referance
3.How to find out the length of the string without using length function
4.How to find out the part of the string from a string
5.difference between throw & throws
He give Programes
1.Reverse a number
2.1,2,3,4,5,65,76,5,,4,33,4,34,232,3,2323,
find the biggest number among these
simple string programe.
what is exception types of exception
From manual
what is the testcase technique
why we write test case.
bug life cycle
what are the different status of bug
what is the different between functional and smoke testing
what is STLC.
from Selenium
what is testng and its advantage
how to handle SSl/
how to handle alert
how to take screenshot
give the diagram write a scrpt..
tell me about Project .What are the challenge face during project
what is the difference between RC and webdriver
what is freamwork explain it.
why we use wait statement.

2nd technical
he gives a application & tell to write the scenario
some manual testing concepts.