Saturday, 7 July 2018

Framework - 


Now its time to create helper method for each page/tab in application. Starting from 
Login Page -> Home Page -> so on



Will write code to verify few stuff in login page and  Home page , same is applied to other pages too.

Note : All these class inherits WebCommon base class.

LoginPage.java

package com.qt.web.automation.helpers;
import com.qt.automation.helpers.WebCommon;
public class LoginPage extends WebCommon
{
    public boolean loginWithValidCredentials()
    {
        webDriverManagerObj.find("name", "username").sendKeys("admin");
        webDriverManagerObj.find("name", "pwd").sendKeys("manager");
        webDriverManagerObj.find("id", "loginButton").click();
        return webDriverManagerObj.find("xpath", ".//*[@id='topnav']/tbody/tr[1]/td[1]/div/div[3]/img").isDisplayed();
    }
  
}
 


HomePage.java

package com.qt.web.automation.helpers;
import com.qt.automation.helpers.WebCommon;
public class HomePage extends WebCommon
{
    public boolean verifyActitimeLogo()
    {
        return webDriverManagerObj.find("xpath", ".//*[@id='topnav']/tbody/tr[1]/td[1]/div/div[3]/img").isDisplayed();
    }
}

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.