Monday 25 May 2020

Enums in java:

Enums in java:
---------------
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma.

public enum TestCaseActivity {

PASS("1"),//public static final TCSTATUS PASS;
FAIL("2"),
SKIP("3"),
MEDIUM_WAIT("3000"),
HIGH_WAIT("6000"),
LOW_WAIT("1000"),
IE("ie"),
CHROME("chrome"),
FIREFOX("firefox"),
EDGE("edge"),
PHANTOMJS("phantom");
private String activity;
private TestCaseActivity(String activity){
this.activity = activity;
}
public String getActivity() {
return activity;
}
}

RegEx Validations utility for PhNum:

RegEx Validations utility for PhNum:
------------------------------------
public interface RegExPhNum {

public static boolean isValidPhNum(String phNum){
boolean flag = true;
String pattern = "(0|91)?[6-9][0-9]{9}";
Pattern pattern2 = Pattern.compile(pattern);
Matcher matcher = pattern2.matcher(phNum);
if (matcher.find()&&matcher.group().equalsIgnoreCase(phNum)) {
System.out.println("It is valid ph num");
}
else{
System.out.println("It is invalid ph num");
flag = false;
}
return flag;
}
}

Current Date Utility Function:

Current Date Utility Function:
------------------------------
public class DateUtility{

static public void selectCurDate(String xpath) throws InterruptedException {
      List<WebElement> dates = getDriver().findElements(By.xpath(xpath));
Date date = new Date();
String curDate = date.getDate()+"";
for(WebElement ele : dates){
String dateTxt = ele.getText();
if (curDate.equalsIgnoreCase(dateTxt)) {
ele.click();
break;
}
}
}
}

Screenshot Utility Function:

Screenshot Utility Function:
----------------------------

In order to take a screenshot we need the following classes & interfaces
1. TakesScreenshot(I)
2. WebDriver(I) | RemoteWebDriver(C)
3. OutputType(I)
4. FileUtils(C)

From java1.8 version onwards inside interface we can take implemented methods as well in the form of default methods and static methods.

public interface CaptureScreenshot {

public static String captureImage(String tcName){
String imgLoc = BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg";
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(imgLoc));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgLoc;
}
public static void captureImage(String tcName,String description){
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Wednesday 22 February 2017

SELENIUM: HOW TO CAPTURE TOOLTIP MESSAGE IN SELENIUM WEBDRIVER:



HOW TO CAPTURE TOOL TIP MESSAGE IN SELENIUM WEB DRIVER:

Tooltip is nothing but an Advice Message to the User (or) Helper Text.
For example In Gmail Signup Page while you are clicking or entering username or Password helper text appears, those text messages are called Tooltip messages.
Steps to read tooltip message:
1. Open browser, enter your application URL (www.gmail.com in this example)
2. Click on the Web element (Password field in this example)and mouse Hover to that tooltip text (Mouse Hover can be performed by using Actions(C) or JavaScript Executor(I)).
3. call get Text() on that mouse hovered Web Element ,store the value in String format and print on the console.
4. Verify with Actual text with Expected result.

Sample Program for reading or capturing the tool tip message :
package com.rameshsoft.demos;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class CaptureTooltipDemo {
          WebDriver driver;
          @Test
          public void mm() throws InterruptedException
          {
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

                    driver=new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().deleteAllCookies();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");

Thread.sleep(2000);

//click on Password
driver.findElement(By.xpath(".//*[@id='Passwd']")).click();;

WebElement ele=driver.findElement(By.xpath("html/body/div[3]/div[1]/div[2]"));

//perfroming mouseHover
mouseHOver(ele);

String ActualText="at least 8 characters. Don’t use a password from another site, or something too obvious like your pet’s name. Why?";

String expectedText=ele.getText();

System.out.println("text is: "+expectedText);

if(ActualText.equalsIgnoreCase(ele.getText()))
{
System.out.println("Both Actual and Expected values are  Equal");        
}
else
{
System.out.println("Both Actual and Expected values are not Equal");   
}
          }
          public void mouseHOver(WebElement ele)
          {
                  
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
         
((JavascriptExecutor) driver).executeScript(mouseOverScript,ele);

                  
          }

}

OUTPUT:
text is: Use at least 8 characters. Don’t use a password from another site, or something too obvious like your pet’s name. Why?
Both Actual and Expected values are  Equal

Friday 17 February 2017

SELENIUM Mousehover by JAVASCRIPT EXECUTOR

SELENIUM Mouse hover by JAVASCRIPT EXECUTOR:
=============================================
package com.rameshsoft;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class MouseOverUsingJS {
 WebDriver driver;
 @Test
 public void test() throws InterruptedException
 {
 System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
  driver=new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
 driver.get("https://www.flipkart.com");

 Thread.sleep(3000);
WebElement ele=driver.findElement(By.xpath("//*[text()='Electronics']"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='4px groove green'",ele);
mouseHOver(ele);
 
 }  
 public void mouseHOver(WebElement ele)
 {
  String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
         
  ((JavascriptExecutor) driver).executeScript(mouseOverScript),ele);
System.out.println("mouse over axtion performed");
 
 }

}

Thursday 16 February 2017

SELENIUM : Extent Reports 2.X



Extent Reports 2.X:
--------------------------
Steps to Work with Extent Reports 2.X:

1. Download  Extent Reports jar file(s),and add to the Project.
open google------>type 'Download Extent Reports'------------>Click on 'ExtentReports for awesome selenium webdriver reporting-------->click on ExtentReports-java-v2 40.2---------->OK

2. Get the ExtentReports(C) object,and give the path of the location where you want to store reports)
Ex: ExtentReports ext=new ExtentReports("C:\\Users\\ramesh\\Desktop\\DemoReports\\report1.html");

3.Get the ExtentTest(C) object
Ex:
ExtentTest logger=ext.startTest("Gmail Test");

4.Log the reports
Ex: logger.log(LogStatus.INFO, "URL is passed");

5.End the Test and call flush().

Ex: ext.endTest(logger);
       ext.flush();
         
Example:

package com.rameshsoft;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class Demo1 {
         
          public static void main(String[] args) throws InterruptedException {                  
                   ExtentReports ext=new ExtentReports("C:\\Users\\ramesh\\Desktop\\DemoReports\\report1.html");
                   ExtentTest logger=ext.startTest("Gmail Test");                  
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\desktop1\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
                   WebDriver driver=new FirefoxDriver();
                   driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);                                                        
                   driver.manage().window().maximize();
                   logger.log(LogStatus.INFO, "Browser is maximized");
                   driver.get("https://www.gmail.com");
                   logger.log(LogStatus.INFO, "URL is passed");

                   driver.findElement(By.id("Email")).sendKeys("atsunithase@gmail.com");
                   logger.log(LogStatus.INFO, "Uname is passed");

                   driver.findElement(By.id("next")).click();
                   logger.log(LogStatus.INFO, "Clicked on Next button");

                   Thread.sleep(2000);
                   driver.findElement(By.id("Passwd")).sendKeys("******");
                   logger.log(LogStatus.INFO, "Password is passed");

                   driver.findElement(By.id("signIn")).click();
                  
                   logger.log(LogStatus.INFO, "Clicked on signIn button");

                   //logger.log(LogStatus.INFO, "closed the browser");
                  
          ext.endTest(logger);
          ext.flush();

          driver.get("file:///C:/Users/ramesh/Desktop/DemoReports/report1.html");
                   }

}

NOTE: If we want we can add screenshots as well to our reports
       ext.addScreenCapture("");