Sunday, 19 February 2017

Selenium Webdriver 7 - Browser manage function "Cookies"


Session highlights 
  • The brief theory about cookies.
  • How to see existing cookies.
  • how to add cookies.
  • how to delete cookies


1. What is a cookie ?
Ans : Cookies are usually small text files.

2. When Cookie file is created?
Ans : Cookies are created when you use your browser to visit a website

3. What cookie file contains?
Ans : "key = value" pair . For ex : "domain=127.0.0.1"

4. What is the use of cookie?
Ans: This is an interesting question. Let's consider below scenarios. 

Scenario 1:
>Open browser.
> type www.google.com and press enter.
> search "Mother Teresa" press enter.
>click on the back button on the browser.
>Now observe page reload the google home page ....! - It means someone is tracking our path.

Scenario 2:
>Open browser.
>Go to gmail website.
>Enter login credentials.
>Click on remember password   ....! - It means someone is remembering.

Thats all ! From above two scenario you might be understood what cookie file contains.


Program:

package shirageri.blog;

import java.util.Set;

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

public class BrowserManage1 {

public static void main(String[] args)
{

String URL = "http://127.0.0.1:88/login.do";
WebDriver driver = new FirefoxDriver();

driver.get(URL);
//Step 1 - Adding cookies 
Cookie cookie = new Cookie("COOKIE_NAME", "Cookie_Value");
driver.manage().addCookie(cookie);
//Step 2 - Displaying cookies
Set<Cookie> cookiesList =  driver.manage().getCookies();
for(Cookie getcookies :cookiesList) 
{
   System.out.println(getcookies );
}
System.out.println("------------------------------");
//Step 3 - Deleting added cookie
driver.manage().deleteCookie(cookie);
//Step 4 - Check deleted cookie is not shown
cookiesList =  driver.manage().getCookies();
for(Cookie getcookies :cookiesList) 
{
   System.out.println(getcookies );
}
System.out.println("------------------------------");
         //Step 5 - Delete all cookies
driver.manage().deleteAllCookies();
}
}

Note : In step 3 from above program we can also use "deleteCookieNamed()" method to delete by passing cookie name as argument. For ex:

driver.manage().deleteCookieNamed("COOKIE_NAME");

Selenium Webdriver 6 - getCurrentUrl() , getPageSource() & getTitle()

Few basic and important browser functions are explained in this session.

1. String getCurrentUrl() : Get a string representing the current URL that the browser is looking at.
2. String getPageSource() : Get the source code of page.
3. String getTitle() : Get the title of the current page.

Note : All these above method returns String value.

Program :

package shirageri.blog;

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

public class HelloBrowser7 {

public static void main(String[] args)
{

String URL = "http://127.0.0.1:88/login.do";

WebDriver driver = new FirefoxDriver();

driver.get(URL);

//1st - getCurrentUrl()
String currentUrl = driver.getCurrentUrl();
System.out.println("Current URl : "+currentUrl);
System.out.println("------------------------------------------");

//2nd - getTitle()
String pageTitle = driver.getTitle();
System.out.println("Page Title : "+pageTitle);
System.out.println("------------------------------------------");

//3rd - getPageSource()
String pageSource = driver.getPageSource();
System.out.println("Page Source : "+pageSource);
System.out.println("------------------------------------------");
}
}


Selenium Webdriver 5 - Opening Application/Website using get() and navigate() method

Note : Please see "Selenium 4" before starting this session.

We start using standalone web application "Actitime" for all our future session.

1. Open Actitme Application in firefox browser. using GET() method.

package shirageri.blog;

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

public class HelloBrowser5 {

public static void main(String[] args) throws InterruptedException {
          
                String URL = "http://127.0.0.1:88/login.do";

WebDriver driver = new FirefoxDriver();

//get() method will open the applicatoin mentioned in its arg section
driver.get(URL );
}
}

2. Open Actitme Application in firefox browser. using navigate().to(URL) method.

package shirageri.blog;

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

public class HelloBrowser6 {

public static void main(String[] args)
{

String URL = "http://127.0.0.1:88/login.do";
WebDriver driver = new FirefoxDriver();
//navigate().to 
driver.navigate().to(URL);
}
}

Note : get() and navigate().to() method perform same work.

Selenium Webdriver 4 - Setup Standalone"Actitime Web application" for Automation

Prerequisite: Install "actitime 3.3MA Setup.exe / or any version of 'Actitime' software". This is a standalone web application. Will use this application for our all automation session,

Installation and Opening actitime Application 
1. INstall application by double clicking .exe file
2. While installing it will ask port. By default, it will take 80.
3. After installation is the success, run "Start actiTime" server by clicking on it, as shown below.

4. After 3rd step is done it will popup window showing status of the server and you can click on button "Open Login Page" which is shown in that pop-up


5. Last step! Make sure that application page is opened successfully as shown below.



Saturday, 18 February 2017

Selenium Webdriver 3 - Closing the browser/browsers using close()

1. Closing Single browser using driver.close().

package shirageri.blog;

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

public class HelloBrowser3 {

public static void main(String[] args) throws InterruptedException {

//Step 1 - Open empty browser
WebDriver driver  = new FirefoxDriver();

//Step 2 -Wait for 5 sec before clossing opened browser
Thread.sleep(1000);

//Step 3 - Close the opened browser
driver.close();


}
}
Note: 
1. Thread.sleep(milisec) - this method makes the running thread to sleep for mentioned timeout. The reason behind using this here is for understandability. 


2. Closing Multiple browsers using driver.close().

package shirageri.blog;

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

public class HelloBrowser4 {

public static void main(String[] args) throws InterruptedException {

//Step 1 - Open 1st empty browser
WebDriver driver1  = new FirefoxDriver();
//Step 2 - Open 2nd empty browser
WebDriver driver2  = new FirefoxDriver();

//Step 3 -Wait for 5 sec before closing 1st browser
Thread.sleep(1000);
//Step 4 - Close the opened browser
driver1.close();
//Step 5 -Wait for 5 sec before closing 2nd browser browser
Thread.sleep(1000);
//Step 5 - Close 2nd browser
driver2.close();
}
}

Note: If you created driver object as an array you can use any looping technique to close it.


Friday, 17 February 2017

Selenium Webdriver 2 - Hello Browser

1. This program will open the single browser and nothing else.

package shirageri.blog;

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

public class HelloBrowser1 {

public static void main(String[] args) {
// TODO Auto-generated method stub

WebDriver driver  = new FirefoxDriver();
}
}


2. Opening multiple browsers

package shirageri.blog;

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

public class HelloBrowser2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

                //Browser 1
WebDriver driver1  = new FirefoxDriver();

                //Browser 2
                WebDriver driver2 = new FirefoxDriver();

                //Browser 3
                WebDriver driver3 = new FirefoxDriver();
}
}



Selenium Webdriver 1 - Environment Setup

This session helps to setup selenium in eclipse windows 7 machine

1. Download the latest eclipse depending on your machine 64 bit or 32 bit from eclipse official site
http://www.eclipse.org/downloads/

2. Download JAVA 7 or JAVA 8 (i.e JDK) depending on need from oracle site
https://java.com/en/download/

3. Install JAVA by double clicking on the Exe file. that is for example"jdk-8u71-windows-x64.exe".

4. Setup environment variable JAVA_HOME.
       i. Right click on my computer. 
       ii. Click on "properties". 
       iii. Click on "Advanced System Setting" from the window opened in the previous step.
       iv. Click on "Environment Variable" button from system properties window.Opened after the previous step.
       v. Under "System variable " section click on "New" button.
       vi.Enter "variable name" as "JAVA_HOME"
       vii. Enter "variable value" as "C:\Program Files\Java\jdk1.8.0_111" ( Write JAVA installed path)
       viii. Click Ok button

5. Click on eclipse icon.

6. Create "New Project" in eclipse.

7. Download "selenium-server-standalone-2.48.2.jar"

8. Import Selenium JAR into the newly created project. [ Importing  JAR into the project: Right click on project -> click on Build path -> Click on Configure build path -> Click on add external jars -> Select selenium jar and click apply button].

9. Check imported jar is seen in the project.

10. Install firefox browser 24.0 version ( Mentioning  browser version because 24.0 is compatible with selenium version 2.48.2)


Thursday, 16 February 2017

IV. Workarround Selenium IDE - PART B.

1. Explaining Selenium IDE component.


  1. Base Url: This is the starting point of the recording. Each time a new recording is started against the different websites, the Url will be added to the Base URL ComboBox, so that it can be used in the future recordings.
This also allows entering the URL directly into the text box.


  2. Speed Slider: This controls the Play-Back speed of the Test Script Execution. The speed can be set by moving/dragging the green round button to the right to make the execution slower. Like wise if moved towards left, the speed of the execution will be faster. This provides quicker access to change the execution speed.
Otherwise, the same task can be done from Menu Item >> Actions >> Fastest. This can be configured to 0 to 9, where 0 is fastest and 9 is the slowest.
3. Play Entire Test Suite: This allows to sequentially play all the Test Cases shown in the Test Case Pane. This is also same as selecting Menu Items >> Actions >> Play Entire Test Suite. But this is easily accessible from Tool Box.
4. Play Current Test Case: This allows to play only the current selected Test Case in the Test Case Pane. This is also same as selecting Menu Items >> Actions >> Play Current Test Case. But this is easily accessible from Tool Box.
5. Pause: This allows to pause the playback. This is also same as selecting Menu Items >> Actions >> Pause / Resume. But this is easily accessible from Tool Box. This button is active only when the Test Execution is in progress.
6. Resume: This allows to resume the playback. This is also same as selecting Menu Items >> Actions >> Pause / Resume. But this is easily accessible from Tool Box. This button is active only when the test execution is paused.
7. Step: This allows to step through the Test Step Commands in the Test Step Pane. This is also same as selecting Menu Items >> Actions >> Step. But this is easily accessible from Tool Box. This button is active only during the play-back time when the test execution is paused.
8. Rollup: This is an advanced functionality. This allows to group commands together and execute them as a single action. It actually bind the test steps in to a function.
9. Record: This allows to start and stop the recording of user actions. The hollow red ball indicates the start of the recording session whereas the solid red ball indicates the end of the recording session. By default, the Selenium IDE opens in the recording mode.

III. Workarround Selenium IDE - PART A.

1.What is IDE?

An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write and test software. Typically, an IDE contains a code editor, a compiler or interpreter and a debugger that the developer accesses through a single graphical user interface (GUI).

Ex: Eclipse is an IDE. Before I explain how this IDE contain compiler inbuilt let's understand JDK and JRE.

Story started when I was installing Eclipse 👶

I got an error when I was installing eclipse in my system, saying that JRE must be configured or installed in the machine.
Next, I installed JRE but how come Eclipse knows i installed JRE? ... For that, We need to create the system variable/User variable JAVA_HOME which contains the path of installed JRE. 
Now, Again 3 new words "system variable ",  "User variable " and "JAVA_HOME"

System variable: are those if you create once it will be available for all users in that system
User variable: Are specific to that user who logged into the system.
JAVA_HOME: Is environment variable.Or you can say its key where other software which required 
JAVA to be pre-installed software. That software will look into this key and find the value set for this key.
Ex: JAVA_HOME = C:\Program Files\Java\jdk1.8.0_111 
Note : Installing JAVA,eclipse and Setting environment varible will explained "Selenium Webdriver 1 - Environment Setup"

Selenium IDE: Is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run in.

Selenium IDE is not only a recording tool: it is a complete IDE. You can choose to use its recording capability, or you may edit your scripts by hand. With autocomplete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer.

Features:
Easy record and playback. 


2. Installing Selenium IDE.


As we know Selenium IDE is plugin for Mozilla Firefox browser. You can install this plugin by 2 ways 

I.Installing throw Firefox marketplace.
Step 1.

Step 2.

Step 3.
Click on install button.

Step 4.
After installing restart the browser or close and re-open.

Step 5. Check IDE icon is shown at right side cornner.

Step 6.
Or you can see IDE under tools section.

II. Install by downloading 

Step1. 
Open Firefox and enter URL https://addons.mozilla.org/en-us/firefox/addon/selenium-ide/

Step 2.
Click on button as shown below.
Step 3.
You will get popup , click on install button.

Step 4.
close and reopen browser.

Step 5. Check IDE icon is shown at right side cornner.


Step 6.
Or you can see IDE under tools section.

II. Selenium Tools.

Selenium

In 2004 Selenium came to the world of software. Mr.Jason Huggins who was working in ThoughtWorks company developed Javascript based record and playback script to speed up his web application manual testing task and he named this as Selenium IDE. And This javascript became core of selenium.

Later from Selenium IDE to Selenium RC. And Selenium RC was most powerful scripting tool during those days.

After that Simon Stewart engineer from Google started working on Webdriver now this also called "Selenium Webdriver"

"Selenium Tool" is also called as "Selenium Automation Suite" because "suit" means the package of many other tools.

Yes! Selenium Automation suit consists of many other tools and they are:

  1. Selenium Integrated Development Environment (IDE).
  2. Selenium Remote Control (RC).
  3. Selenium WebDriver.
  4. Selenium Grid.

I. Selenium Integrated Development Environment (IDE).
IDE - Means an integrated development environment. Is a software application that provides comprehensive facilities to computer programmers for software development.

From above IDE definition it's clear that using IDE we can develop any software. For example "Eclipse" is IDE.

Selenium IDE: Selenium IDE has a recording and playback feature, which records user actions as they are performed and then exports them as a reusable script in one of many programming languages that can be later executed.

Advantages of Selenium IDE: Very simple to automate web application because it's just record and playback. No need to write any code in Selenium IDE. The code is generated automatically by IDE as when you start recording tests.

Disadvantages of Selenium IDE: It is not designed to run your test passes nor is it designed to build all the automated tests you will need. Specifically, Selenium IDE doesn’t provide iteration or conditional statements for test scripts. 

II. Selenium Remote Control (RC).
Selenium RC was the main Selenium project for a long time, before the WebDriver.WebDriver is also called Selenium WebDriver .Releases of WebDriver are  Selenium1 and Selenium2.

Selenium 1 is still actively supported (mostly in maintenance mode) and provides some features of RC



Selenium RC involves "Remote Control" Server need to be started for automating any web application. RC is behaving as a middle layer which takes all commands from user-written code and interprets and communicate with the web application.

 From Above diagram it's clear that Code is written in eclipse which communicates with RC server then RC interacts with the web application for intended operation.

III. Selenium WebDriver.
In the world of Automation implementation of Selenium Webdriver is the biggest change. In the market its also know as "Webdriver / Selenium Webdriver"

Latest version of Selenium webdriver is "Webdriver 3.0"  released in October 2016.

But in this tutorial, we will work on Selenium Webdriver 2.0 as its more stable than the 3.0.

Note: Advantage of Selenium Webdriver over RC is no concept of a Server here.

IV. Selenium Grid.
Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.

Note: Selenium Gride is totally different then Webdriver/RC/IDE. 
Please refer: http://www.seleniumhq.org/docs/07_selenium_grid.jsp for more details 

I. Introduction to Automation.

Introduction

In this post, we learn

1. What is Automation?
2. Why Automation?
3. When we go for automation?
4. Which all available tools for automation?
5. Which type of Applications can be automated?
6. Which is the best tool for automation?

1. What is Automation?
Answer: Anything task which can be completed without manual intervention is called automation.
For example "Washing machine".

2. Why Automation?
Answer: There are many reasons why we go for automation. It depends on domain/field. If we think in software testing field, 
a. Automation will reduce a time taken to test software.
b. Accuracy is high.
c. No manual intervention. Just automate and run anytime from anywhere.
3. When we go for automation?
Answer: When the same manual testing task is repeating many time then we go for automating such application.

4. Which all available tools for automation?
Answer: Now this also depends on the type of application. So let's see which all types of application usually we get to work in any software company.
There are 3 types of application
  • Stand-alone application – software installed on one computer and used by only one person. For ex – Installing s/w of a Calculator, Adobe Photoshop, MS Office, AutoCad. Standalone applications are further classified based on operating system like Linux and Windows.
  • Web Application – any application software accessed through the browser is called web application. For ex – yahoo.com, gmail.com
  • Client – Server application – here, we are installing both client and server software to access the application. 
Note: In some case "Client - Server Application" is also a kind of "Web Application". In that case, browser behaves as a client.

There are many tools and few are listed below

  • Selenium
  • TestingWhiz
  • HPE Unified Functional Testing (HP – UFT formerly QTP)
  • TestComplete
  • Ranorex
  • Sahi
  • Watir
  • Tosca Testsuite
  • Telerik TestStudio
  • WatiN
Note: QTP is used to automate "Standalone App" as well as "Web App". The disadvantage is it's not free source/ Open source tools. A user who want to work on QTP tool he/she need to purchase it from HP.

Note: Selenium is open source tool or its freely available. This is the reason most of the user/company prefer to use selenium to automate web applications.

5. Which type of Applications can be automated?
Answer: Any application can be automated.It may be Linux based or windows based

6. Which is the best tool for automation?
Answer: Any tool which is user-friendly and open source and more powerful (Powerfull I mean here, a lot of features in one tool).
For Ex:  Selenium. It's Open Source and freely available. The best tool for Web application automation.
   


Selenium Course Outline

I.   Introduction to Automation.
     1. What is Automation?
     2. Why Automation?
     3. When we go for automation?
     4. Which all available tools for automation?
     5. Which type of Applications can be automated?
     6. Which is the best tool for automation?

II.  Selenium Tools.
     1. Selenium Integrated Development Environment (IDE).     
     2. Selenium Remote Control (RC).
     3. Selenium WebDriver.
     4. Selenium Grid.

III. Workarround Selenium IDE - PART A.  
   1. What is IDE.
   2. Installing Selenium IDE.
   
 
IV. Workarround Selenium IDE - PART B.  
   1. Explaining Selenium IDE component.





Sunday, 22 January 2017

MongoDB Session 2 : Miscellaneous things during start MongoDB server

Keep below points in mind before starting MongoDB

1. Create folder "data" inside that another folder "db" in the same drive where you installed MongoDB
2. Installing drive should contain free space more than 3GB
Reason: MongoDB stores all DB details in c:\data\db  ( Check path where you created folder "data\db")
Note: try to create data\db folder on the same drive where you installed MongoDB software.

What if space is not available ...? hmm you will get below error while starting MongoDB server



3.You have to open two cmd prompt one for starting MongoDB server and second for opening MongoDB shell prompt.
Note: Don't close the first cmd window. If you close then the server will stop.
4.If you facing problem in starting server then check in case data/db folder contains MongoDB.lock file. if so it means MongoDB server is already running and you can't run that again.

Error message looks like 

Saturday, 21 January 2017

MongoDB Session 1 : Installing MongoDB For Windows 7

1. Download mongodb for win7 from MongoDB official website
    https://www.mongodb.com/


2. Install downloaded .exe file by double clicking. 
         This is as simple as any .exe installation . Just remember in which drive you are installing
3. Check installed directory




4.Create  "data/db" empty folder in same drive where you installed Mongo. In my case its C:
         i.e "C:/data/db".
         
  • Reason: MongoDB stores temp,DB related files in this folder. It will throgh error message if it dint find these folders.
  • Temp/DB Files? :  Yes! it stores some mongoDB related files. For ex : if I create "Student DB" and in that I create some collections. Then two files will be generated in                   "C:/data/db" . i.e student.0 and student.ns.
  • Comparing RDBMS terminology with NoSQL DB/Unstructured DB (MongoDB).
         Data Base                 : Data Base. 
         Tables                      : Collections.
         Rows & Columns     : Documents/Records.
 
5. Run MongoDB server 
         You have to start MongoDB server before performing any DB operation. This is same as any              other relational DB . That is if you are using Oracle DB , then Oracle DB must be running                  then you can connect to it by using terminal (SQL+) or any sql editior (SQL Developer).
  • Open new command prompt : Type "cmd" in Run prompt.
  • change path to installed directory . i.e "cd C:\Program Files\MongoDB 2.6 Standard\bin".
  • type "mongodb" and press enter
        
        Once you see the control stops displaying any logger and without any error message it means MongoDB is started.
Note : Dont close this window. If you close then server will stops running.

6. Opening MongoDB prompt/Shell to perform DB operation
  • Open new command prompt : Type "cmd" in Run prompt.
  • change path to installed directory . i.e "cd C:\Program Files\MongoDB 2.6 Standard\bin".
  • type "mongo" and press enter

Thats All..! You are done with instalation and opening Mongo Shell.


Starting Appium through Automation

Prerequisites : Hope you already know selenium and TestNg and your eclipse is set for these.
We need few extra jars to achive this appium starting through java code. so download below jars and add it to build path.

1. gson-2.8.0.jar
2. java-client-3.2.0.jar
3. commons-validator-1.4.0.jar


Code:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class AppiumStartStop
{

AppiumDriverLocalService service;

@BeforeClass
public void init()
{
System.out.println("Starting Appium ... !");

service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("C:/Program Files/nodejs/node.exe"))
.withAppiumJS(new File("C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js"))
.withLogFile(new File("d:/logs.txt")));

service.start();
}

@Test
public void appiumTesting() throws MalformedURLException
{

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName","Appium");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "6.0.1");
capabilities.setCapability("deviceName","Manjus");
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", "HomeScreenActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("https://shirageri.blogspot.in");
}

        @AfterClass
public void tearDown()
{
System.out.println("Stopping Appium ... !");
service.stop();
}

}