Automation Using Selenium Webdriver

Friday 21 October 2016

Download And Save Image

Download And Save Image Using Selenium WebDriver + Actions + Robot (1)

Interaction API tutorial section. For downloading Image, We will use same WebDriver Actions class with java robot class.
For saving Image from web page In selenium webdriver, We have to perform bellow given actions.

    Right click on Image
    Select "Save Image As" option from mouse right click context menu.
    Enter file name In save Image dialog
    Press Save button.

Right click on Image using contextClick() method of Actions class
As you know, "Save Image As" option will display when you right click on any Image. We will use contextClick() method of WebDriver Actions class to right click on Image as shown In bellow Image.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Actions_Test {

 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://any website");
 }

 @Test
 public void Save_Image() throws IOException, InterruptedException, AWTException {
  //Locate Image
  WebElement Image =driver.findElement(By.xpath("//img[@border='0']"));
  //Rihgt click on Image using contextClick() method.
  Actions action= new Actions(driver);
  action.contextClick(Image).build().perform();

  //To perform press Ctrl + v keyboard button action.
  action.sendKeys(Keys.CONTROL, "v").build().perform();

  Thread.sleep(3000);
  Robot robot = new Robot();
  // To press D key.
  robot.keyPress(KeyEvent.VK_D);
  // To press : key.
  robot.keyPress(KeyEvent.VK_SHIFT);
  robot.keyPress(KeyEvent.VK_SEMICOLON);
  robot.keyRelease(KeyEvent.VK_SHIFT);
  // To press \ key.
  robot.keyPress(KeyEvent.VK_BACK_SLASH);
  // To press "test" key one by one.
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyPress(KeyEvent.VK_S);
  robot.keyPress(KeyEvent.VK_T);
  // To press Save button.
  robot.keyPress(KeyEvent.VK_ENTER);
 }
}
}

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.



Functional Testing Interview Questions


functional testing interview questions and answers

functionality is not working on What to do when most important the date of release?


If the release will be major, It should be rollback. Tester should create a bug for the issue.
If already fixed on previous release, we should reopen the bug. If the release is minor
It should be con...
or
As per my opinion, if the major functionality is not working then that build should be rejected and
 previous build should be deployed again (if in current build major fixes are not there) and then
we can send the build to client and later on we can perform hot fixes for other issues

Have you implemented Agile or Waterfall methodology in your project?

We have implemented using Agile. In Agile we have sprints, in which each sprint have 2 week duration oftime, within which a part of the deliverable have to be covered.
What is the need of testing functional part of an application when structural part is tested properly?
Functional testing means testing the every components and features and modules and also test
the data is transferred correctly between modules. Structural testing is on inside part of the software
             or
To see According to the requirement each and every components or fields are properly working or not for that purpose we need a functional testing.

What is the difference between V-model and Fish model?

In Fish model every stage is tested by other team for completeness and correctness.

In V model every stage same person reviews but in last stage software testing will be done by other testers.
V-model is methodology wherein testing and development goes paralley in iterative and incremental manner
What are the test cases for testing an "add to cart button"??

The fifth test case what have you mentioned that "When there is nothing in the cart click on add to cart button"
First think is that the add to cart button wont appear simply anywhere..
On selecting the particular product itself "Add to cart button" appears
1. Click on the Add to card Button without adding any item. 2. Click multiple times on Add to cart.
3. Add 10, 20, 30,...100 items and click on Add to cart, and at same time click multiple times.
4. S...
Are both 'Bakward Testing' & 'Regression Testing' one and the same? If Not, What is the difference between them?
Backward testing is retesting a specific module or area by seeing the defect or
bug which has raised in those perticular modules.
 or
I'm guessing you mean backward compatibility testing?? Given multiple version releases,
an application created on a new version should still work with the older versions making
it backwards compatibl...

What all do you report at the END of Testing?

Test Summary report is sent at the end of a project which consist of details like number of
test scripts executed , number of defects found and also the scope which was removed during testing

Functional and Functionality testing?

Functional testing focus on the output is as per the functioanlity or requirement.
 Functional and Functionality testing is same

Telecom - Equivalence class portioning?

There is a customer who has taken a service in which he is getting 300 free minutes.
So in this case make a call 1-300 , 300-10000(max limit) . So here we have 2 equivalent classes.
You can test 250 minutes and 400 minutes.. to test if both the classes data is working fine

Manual Test Case for Login page?

Here is the template below. I would like to see the detailed test cased for login page
column 1 is the "test case ID number"
column 2 is the "test case name"
column 3 is the "test objective"
column 4 is the "test conditions/setup"
column 5 is the "input data requirements/steps"
column 6 is the "expected results"
writing test case for login username (name must be 1-40 char,min-4 char) testcase id-tc1 testcase
name-username test objective-login username test condition -login page should be opened test data-fo.
..
Acceptance Testing?

The main focus of acceptance testing is:a) Finding faults in the systemb) Ensuring that
the system is acceptable to all usersc) Testing the system with other systemsd) Testing from
a business perspective.

or
Main focus of Acceptance testing to see whether all the given requirement is implemented in
application or not as well as to find the defect

How do you test when No time?

There is a new build and you have not much time left behind to test(manual test), what will you do?

It is too late but still we cant perform adhoc testing when time is less, as it requires different
approaches for testing the application to find bug, to increase the bug count. So for testing an appl..

If there no sufficient time to test the application then immediately we will go for exploratory testing...

Exploratory testing is dynamic testing type and it tests the entire application simultaneously
while creating test design and test execution is possible.

What is the difference between Agile methodology and Waterfall model?

In Agile methodologies accepts "Dynamic changes". Agile is modern methodology where a 5 to 10 people gathered at one place and to deliver a working software with in a given short span of time i.e, 2 ...

What is the difference between smoke and sanity testcase?

Smoke testing is a black box testing technique and it test after immediate the build release.

Smoke test touches all the areas of the application functionality with out going into deep.

Smoke test reveals that any simple failure in the application and application is ready for further testing.

or

Smoke testing is a shallow and wide approach whereby all areas of the application without getting into too deep, is tested.

A sanity test is a narrow regression test that focuses on one or a few areas of functionality. Sanity testing is usually narrow and deep.

What type of Testing you will do once the FRD is in place and approved?

FRD stands for functional Requirement Document. The first type of testing you would do when
the FRD is ready and approved is STATIC TESTING. This involves studying the work documents
like the Requirem...

What do you mean by a dependent functionality in a build?

Functional testing: testing each and ever functionality in the application whether it is working
properly or not giving valid information to the implication,and invalid information to the application,functionality is a black box testing.

or
Functional testing is a type of black box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered (Not like in white-box testing)