Wednesday, 16 May 2018

Interview question | Page Factory | Lazy loading

Here is the interview question:

As per "Page Factory", all the elements will be initialized at the time of page load.
But in dynamic applications how page factory works?
Ex: Assure a drop down content ( options)  loads on click of the respected select box, how can you implement page factory for this?, as page factory initializes elements at the time of page load only.

Answer:
Lazy load concept.

AjaxElementLocatorFactory is a lazy load concept in Page Factory pattern to identify WebElements only when they are used in any operation
i.e. a timeOut for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory.

Syntax : ( similer to plain "Page Factory init elemnts"  method )

PageFactory.initElements(new c(driver, 30), this);

If element not found in 30 secs, NoSuchElemementException throws.

happy coding.

Thursday, 3 May 2018

query HTML String using jsoup

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

Public Class Test
{

public void m1(){
try {
URL myUrl = new URL("https://www.hello.com/abc/abo.php");
URLConnection yc = myUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
String str1 = a.toString();
Document parsedHtmlFragment = Jsoup.parse(str1);
Elements matchingLIs = parsedHtmlFragment.select("#ID");

for (Element matchingLI : matchingLIs) {
cGuid = matchingLI.text();
}

} catch (Exception e) {
// TODO: handle exception
} // TODO Auto-generated method stub
}