Friday, July 24, 2020

PageFactory CacheLookup(@CacheLookUp.)

We could ‘cache’ the element once we’d looked it up and save some time of execution by commanding PageFactory to not search the WebElements on the page again and when we know that the element is always going to be there and won’t change

Any attribute marked [CacheLookup] will not be searched over and over again – this is especially useful for elements that are always going to be there (not always true for AJAX apps). It means the elements of the page will be cached once searched. All elements used in the HomePage & LoginPage class are static and are always present. So it is better to cache objects and save execution time of the test run.


When the webpage has static elements, caching the element can help in multiple ways. When the elements are cached, it doesn’t have to locate the elements again on loading the page, instead, it can reference the cached element repository. This saves a lot of time and elevates better performance.

Pagefactory provides this feature of caching the elements using an annotation @CacheLookUp.
The annotation tells the driver to use the same instance of the locator from the DOM for the elements and not to search them again while the initElements method of the pagefactory prominently contributes to storing the cached static element. The initElements do the elements’ caching job.

Website: Linkedin.com
// Locator for Email Address
@CacheLookup
@FindBy(id = "username")
private WebElement userName;

// By EmailAddress = By.xpath("//input[@id='username']");

// Method to type EmailId
public void typeEmailId(String userName1) {
long NoCache_StartTime = System.currentTimeMillis();
for (int i = 0; i < 3000; i++) {
driver.findElement(By.xpath("//input[@id='username']"));
}

long NoCache_EndTime = System.currentTimeMillis();
long NoCache_TotalTime = (NoCache_EndTime - NoCache_StartTime) / 1000;
System.out.println("Response time without caching Searchbox " + NoCache_TotalTime + " seconds");

// cached element
long Cached_StartTime = System.currentTimeMillis();
for (int i = 0; i < 3000; i++) {
userName.getLocation();
}
long Cached_EndTime = System.currentTimeMillis();
long Cached_TotalTime = (Cached_EndTime - Cached_StartTime) / 1000;
System.out.println("Response time by caching Searchbox  " + Cached_TotalTime + " seconds");

On execution, we will see the below result in the console window:
Launched chrome browser
Environment : https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin
Response time without caching EmaailBox 33 seconds
Response time by caching EmaailBox  23 seconds
PASSED: linkedInLogin

Note: 
1.@CacheLookUp mainly used to find the element directly without scanning complete page
2.@CacheLookUp will work web element static or xpath, css selector is static.

Similarly AjayElementLocator class is same like WebDriverWait in Selenium WebDriverWait will wait until elements is visible based on our condition Similarly AjayElementLocator class is used to wait until Ajax related elements visible If Appln is not Ajax Then no use of AjaxElementLocator class

Pagefactory just like the usual POM is a wonderful concept to apply. However, we need to know where the usual POM is feasible and where Pagefactory suits well. In the static applications (where both XPath and elements are static), Pagefactory can be liberally implemented with added benefits of better performance too.

Alternatively, when the application involves both dynamic and static elements, you may have a mixed implementation of the pom with Pagefactory and that without Pagefactory as per the feasibility for each web element.

=================================================

Login Page

/** * */ package Pages; import static configs.AutoConfigConstants.userName1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.CacheLookup; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory; import base.utils.BasePage; import configs.AutoConfigConstants; /** * @author manjunathk Jul 24, 2020 */ public class LoginPage extends BasePage { WebDriver driver; public LoginPage(WebDriver driver) { super(driver); this.driver = driver; // PageFactory.initElements(driver,this); PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this); } By signIn = By.xpath("//button[contains(text(),'Sign in')]"); By username = By.xpath("//input[@id='username']"); By clickContinue = By.xpath("//span[contains(text(),'Continue')]"); By password = By.xpath("//input[@id='password']"); By clickLogOutBar = By.xpath("//body/div[@id='root']/div/div[@id='SW']/div/div/ul/li[6]"); By singOffBar = By.xpath("//img[@class='nav-item__profile-member-photo nav-item__icon ghost-person ember-view']"); // By password = By.xpath("//input[@id='password']"); By singOut = By.xpath("//a[text()='Sign out']"); String emailId; public LoginPage loginApplication() { // safeClick(loginBtn); typeEmailId(userName1); // safeType(username, AutoConfigConstants.userName); // safeClick(clickContinue); safeClick(password); // waitForPage(800); safeType(password, AutoConfigConstants.passWord); safeClick(signIn); // waitForPage(800); safeClick(singOffBar); safeClick(singOut); // waitForPage(800); return this; } public LoginPage loginApplication(String userName, String passWord) { safeType(username, userName); safeClick(password); safeType(password, passWord); safeClick(singOffBar); waitForPage(800); return this; } // Locator for Email Address @CacheLookup @FindBy(id = "username") private WebElement userName; // By EmailAddress = By.xpath("//input[@id='username']"); // Method to type EmailId public void typeEmailId(String userName1) { long NoCache_StartTime = System.currentTimeMillis(); for (int i = 0; i < 3000; i++) { driver.findElement(By.xpath("//input[@id='username']")); } long NoCache_EndTime = System.currentTimeMillis(); long NoCache_TotalTime = (NoCache_EndTime - NoCache_StartTime) / 1000; System.out.println("Response time without caching EmaailBox " + NoCache_TotalTime + " seconds"); // cached element long Cached_StartTime = System.currentTimeMillis(); for (int i = 0; i < 3000; i++) { userName.getLocation(); } long Cached_EndTime = System.currentTimeMillis(); long Cached_TotalTime = (Cached_EndTime - Cached_StartTime) / 1000; System.out.println("Response time by caching EmaailBox " + Cached_TotalTime + " seconds"); userName.sendKeys(userName1); } }


==================================
class LoginLogout


package automationTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import Pages.LoginPage; import base.utils.BaseTest; public class LoginLogout extends BaseTest { @BeforeTest public void beforeTest() { launchFoxApplication(); getInstanceOfWebPages(LoginPage.class).loginApplication(); } @Test public void linkedInLogin() throws InterruptedException { //getInstanceOfWebPages(HomePage.class).clickSignOff(); /* * Assert.assertEquals( * driver.findElement(By.xpath("//span[contains(text(),'India')]")) * .getText(), "India"); */ } @AfterTest public void teardown() { getDriver().quit(); } }



Monday, July 20, 2020

ExtentReport

Problem

I want Beautiful report tool  where my management gets impressed :-)

Build your own utility


Steps:

1.https://mvnrepository.com/artifact/com.aventstack/extentreports

add below dependency in pom.xml

<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.1.7</version>
</dependency>





Create a new class with ExtentReporterNG


package ExtentReport.ExtentReports;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class ExtentReporterNG {
static ExtentReports extent;

public static ExtentReports getReportObject() {
// ExtentReports ExtentSparkReporter
String path = System.getProperty("user.dir") + "\\reports\\index.html";
ExtentSparkReporter reporter = new ExtentSparkReporter(path);
reporter.config().setReportName("DemoReport");
reporter.config().setDocumentTitle("DemoTitle");

extent = new ExtentReports();
extent.attachReporter(reporter);
extent.setSystemInfo("Tester", "Manju");

return extent;
}

}


Go through TestNG Listener class





Note:

Add testng.xml with test class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<listeners>
    <listener class-name = "ExtentReport.ExtentReports.Listeners"></listener>
    </listeners>

<test thread-count="5" name="Test">

<classes>
<class name="ExtentReport.ExtentReports.DemoReport" />
<class name="ExtentReport.ExtentReports.TestFail" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->





Selenium_Grid_With_Docker_Compose(yml file)

 docker-compose.yml version: "3" services:   hub:     image: selenium/hub:3.141.59     ports:       - "4444:4444"   chro...