Friday, 24 May 2019

stop loading images for Functionality testing. - For quick test execution

For Automation scrips to run faster, we can completely avoid loading images on to browser - which internally saves lot of time for execution.

Do you wanted to check how? 

here you go.




For Chrome Browser:

Step 1:
Map<String,Object> images = new HashMap<String,Object>();
images.put("images",2);

Step 2:
Map<String,Object> prefs = new HashMap<String,Object>();
prefs.put("profile.default_content_settings_values",images);

Step 3:
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption("prefs",prefs);

Step 4:
WebDriver driver = new ChromeDriver(options);


For FireFox Browser:

Step 1:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.image",2);

Step 2:
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
options.setCapability(FirefoxDriver.PROFILE,profile);

Step 3:
WebDriver driver = new FirefoxDriver(options);

Thursday, 9 May 2019

Interview | @CacheLookup - in Page Object Model | Page Factory


@CacheLookup, as the name suggests helps us control when to cache a WebElement and when not to.

This annotation when applied over a WebElement instructs Selenium to keep a cache of the WebElement instead of searching for the WebElement every time from the WebPage. This helps us save a lot of time.

Syntax:
 @FindBy(how = How.NAME, using = "firstname")
 @CacheLookup
 public WebElement firsNameCached;

Tuesday, 7 May 2019

Interview | RestAssured | Gpath | Groovy GPath in REST Assured,

Easy way to query Json Response object using - inbuilt feature of Rest Assured - Gpath

API :http://www.test.com/teams/88
Resp:
{
  "id": 88,
  "name": "Selenium"
}


@Test
public void gpath_extractSingleValue_findSingleTeamName() {
Response response = RestAssured.get("teams/88");
    String teamName = response.path("name");
    System.out.println(teamName);
}

A quick note on the .path() method. How did REST Assured know to use JSONPath, instead of XMLPath? We didn’t specify it! That’s some of the magic of GPath in REST Assured


@Test
public void gpath_extractSingleValue_findSingleTeamName_responseAsString() {
    String responseAsString = get("teams/88").asString();
    String teamName = JsonPath.from(responseAsString).get("name");
    System.out.println(teamName);
}

@Test
public void gpath_extractSingleValue_findSingleTeamName_getEverythingInOneGo() {
    String teamName = get("teams/88").path("name");
    System.out.println(teamName);
}

Other Samples:
Fist Child - response.path("teams.name[0]");
Last Child - response.path("teams.name[-1]");
All Childs -  ArrayList<String> allTeamNames = response.path("teams.name");
All Parents Data - ArrayList<Map<String,?>> allTeamData = response.path("teams");
Query - object.teams.find { it.name == 'Leicester City FC' }

RestAssuredAssertion :

@Test
public void gpath_extractSingleValue_findSingleTeamName_useAssertion() {
    given().
    when().
            get("teams/66").
    then().
            assertThat().
            body("name", equalTo("Manchester United FC"));
}

Interview | How can you solve StaleElementReferenceException

How can you solve StaleElementReferenceException

Answer : ExpectedConditions.refreshed

Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.


public WebElement waitForElementToBeRefreshedAndClickable(WebDriver driver, By by) {
    return new WebDriverWait(driver, 30)
            .until(ExpectedConditions.refreshed(
                    ExpectedConditions.elementToBeClickable(by)));
}

So basically, this is a method that waits until a DOM manipulation is finished on an object.

When the DOM has manipulated, and say after clicking a button, adds a class to that element. If you try to perform an action on said element, it will throw StaleElementReferenceException since now the  WebElement returned now does not represent the updated element.

You'll use refreshed when you expect DOM manipulation to occur, and you want to wait until it's done being manipulated in the DOM.

Interview | Can we have multiple finally blocks

Answer is NO.



try (1)
catch (0 or more)
finally (0 or 1)


- try block must be followed by either catch or finally block.
- When an exception occurs, that exception occurred is handled by catch block associated with it.
- Java catch block is used to handle the Exception. ...
- This ensures that the finally block is executed even if an unexpected exception occurs.