Search This Blog

Breaking

Tuesday, 28 September 2021

September 28, 2021

[Selenium 4.0 ] -> Relative Locator example from Google Home page

Though there are many exciting features available in Selenium 4.0 (Release candidate is available as of now), But I am exciting about this new concept of Relative locators for finding elements. 

Relative locator is a very easy to understand concept where we try to identify a locator with respect to it's position on page. 















So if you have a locator available for a field , and you can see on web page that there is a one more field available left/right/above/below of this field, you can use relative locator concept to identify that element.

I tried to use these new locators in Google home page and I almost able to use these locator there. So let me share, how I used these locators in Google home page:

1. toRightOf() : As it's name insinuates, we can identify an element which is placed right of a particular element.

Use Case: Find the locator of button who is available right to "Google Search button"

  @Test
    public static void toRightOfLocator() {
        WebElement googleSearch = driver.findElement(By.xpath("(//input[@name='btnK' and @type='submit'])[2]"));
        
System.out.println("1: Google search button text value is: " +  googleSearch.getAttribute("value"));
        
WebElement imFeelingLucky = driver.findElement(RelativeLocator.with(By.xpath("//input")).toRightOf(googleSearch));
        
String text = imFeelingLucky.getAttribute("value");
System.out.println("2: I am feeling lucky which is right to Google search button text value is: " +text);
    }

As you can see in above code, First I try to find  "Google Search" button locator, Then , with the help of toRightOf() locator, I tries to find, "I'm Felling Lucky" button locator. 

Here, we need to notice, I gave relative locator element type, which is Input.

So the entire code executes in this manner:

Find the input element which is in right of "Google Search" input button.

2. toLeftOf(): As it's name insinuates, we can identify an element which is placed left to a particular element. The code is almost similar to above one, just vice versa.

Use Case: Find the locator of button who is available left to "I am feeling button"

@Test
    public static void toLeftOfLocator() {
        WebElement imFeeling = driver.findElement(By.xpath("(//input[@name='btnI' and @type='submit'])[2]"));
        System.out.println("1: I am feeling lucky button text value is: " + imFeeling.getAttribute("value"));
        WebElement googleSearch =driver.findElement(RelativeLocator.with(By.xpath("//input")).toLeftOf(imFeeling));
        String text = googleSearch.getAttribute("value");
        System.out.println("2: Google search which is left to I am feeling lucky button text value is: " + text);
    }

3. below() : With the help of this relative locator concept, we can find element below to a particular element.

Use Case: Find the first button present in google home page below to Google search field

@Test
    public static void belowLocator() {
        WebElement googleSearch = driver.findElement(By.xpath("(//input[@name='q' and @type='text'])"));
        WebElement imFeelingLucky =driver.findElement(RelativeLocator.with(By.xpath("//input")).below(googleSearch));
        String text = imFeelingLucky.getAttribute("value");
        System.out.println("1: First input field under google search field is: " + text);
    }

Here, I tried to find input field which is below to google search field. It picked the first element. If you think, there will be many elements, then use findElements instead of findElement.

4. above()

Use Case: Find input element above to "I'm Feeling Lucky" Button and  insert a search string into it.


 @Test
    public static void aboveLocator() {
        WebElement imFeeling = driver.findElement(By.xpath("(//input[@name='btnI' and @type='submit'])[2]"));
        WebElement search =driver.findElement(RelativeLocator.with(By.xpath("//input")).above(imFeeling));
        search.sendKeys("QA Automation ");
    }

5. findElements() with relative locator: So if you need to find links, buttons, text after a particular fields, then you can use findElements() after the relative locator. It will be very helpful.


Use Case: Find all links text in Google home page below to "I'm Felling Lucky button"


  @Test
    public static void allLinksBelowCertainField() {
        WebElement imFeeling = driver.findElement(By.xpath("(//input[@name='btnI' and @type='submit'])[2]"));
        List< WebElement> allLink =driver.findElements(RelativeLocator.with(By.xpath("//a")).below(imFeeling));
        for (WebElement webElement : allLink) {
            System.out.println(webElement.getText());
        }
    }

Similarly you can use it for right, left and above to a particular element.

6. near(): One more interesting locator, which I found is near locator. near locator, as it's name indicates used to identify element near to a particular element with the help of pixels.

To use this locator, I used our own blog, as in Google home page, I could not find any straightforward use case.

Use Case: Find element next to icon "BreakingNews"

  @Test
    public static void nearLocator() {
        WebElement breakingIcon = driver.findElement(By.xpath("//i[@class='fa fa-fire']"));
        WebElement breakingText =driver.findElement(RelativeLocator.with(By.xpath("//span")).near(breakingIcon));
        String text = breakingText.getText();
        System.out.println("1: Text near to breaking icon is: " +text);
    }

Let me know if you need the working code for this. I will share the github repo for this.

Friday, 24 September 2021

September 24, 2021

Ashot library to your visual automation in java based project

Though I was very impressed last time when I used webdriverIO visual automation capability, but the flakiness which comes with it, always made me skeptical about relying on it much. And now, when I am again back to java based automation framework, I feel it to give it a try one more time. 

So after searching about the library which can give support the visual capability for a java based project, I came across the Ashot library. I added its dependency in my maven project and started working on a POC. I tried to do a comparison for a static page, specific web element and then a page having dynamic content.










In my local machine, I didn't face any issue, and it worked great. But the one thing, which disappointed me is their support. Looks like, team is not actively maintaining the code. And it might be, that if you will stuck at some place, you wouldn't find support much. 

So if you have a java maven project, add Ashot maven dependency.

<dependency>

    <groupId>ru.yandex.qatools.ashot</groupId>

    <artifactId>ashot</artifactId>

    <version>1.5.4</version>

</dependency>


For capturing an image, you can use below method:

new AShot()
  .shootingStrategy(ShootingStrategies.viewportPasting(100))
  .takeScreenshot(webDriver);


Keep this image in a folder, and then run it again and use it's difference method to find out the difference between the 2 images.

ImageDiff diff = new ImageDiffer().makeDiff(myScreenshot, anotherScreenshot);
You can read the documentation about this project to get more detail about it. And if you have any question,Then let me know. I will be happy to share my learning.
September 24, 2021

Sonar lint [A must have tool] for your automation project and team

 Recently I feel the need to have a static code analysis of my one of the java automation project. I was aware about the sonar qube and sonar lint but I never implemented or used these tool in any of my automation project. Though in one of my Typescript bases project we have used TSLint which was very impressive. 

So I started googling about it. And picked one of my sample project to use these tools. Let me first differentiate between these 2. 

Sonar lint lives only in the IDE (IntelliJ, Eclipse and Visual Studio). Its purpose is to give instantaneous feedback as you type your code. For this, it concentrates on what code you are adding or updating.

SonarQube is a central server that processes full analyses (triggered by the various SonarQube Scanners). Its purpose is to give a 360° vision of the quality of your code base. For this, it analyzes all the source lines of your project on a regular basis.

So I preferred to use sonar lint in one of my project. As usual, I added this in my favorite IDE intelij. Once it get installed, you can see an option sonar Lint in the bottom as an option,





It keeps showing you code smells with explanation and a possible solution. 

For example:






This helps you to reduce lots of small issues which we simply missed because of unawareness and our ignorance. We can run this for the whole project also, though it takes some time to get complete if its a large project.

Sonar lint is available for eclipse and visual studio as well.

Thursday, 16 September 2021

September 16, 2021

[Fix] -> Clicking on elements is failing in Jenkins

 Recently, I encountered a weird issue where in my local machine, all my UI Automation scripts were working perfectly fine but when same ran on Jenkins machine, they started failing by giving exception: Element is not clickable at this point. I first thought, it might be happening because of different resolution of Jenkin machine (which still a possible reason) but unfortunately, I didn't have access to that Jenkin machine where it was failing.

To fix this, I tried to change the selenium click method to java script executor click method. And, it worked.













WebElement elem = driver.findElement(By.xpath("your xpath"));

JavascriptExecutor executor = (JavascriptExecutor) driver;

executor.executeScript("arguments[0].click();", elem);


So the second thing which I did was changing all click method to java script executor click. But then I identified, that few of the clicks are not working.

Those were the elements which were present as Button in the webpage. So I didn't change that click.

And It resolved.

Wednesday, 15 September 2021

September 15, 2021

[Workfront] -> Workfront API to retrieve task/request/project field and custom form values

 This post is in continuation of my last worlfront post. So after creating request, the next operation is to validate that with whatever field values, we have created the request, same is reflecting there or not. The same validtaion, we need to perform after converting this request to project as well. 

So to perform both these validations, we need to fetch the field values from API response. Below are the end points which can be sued for fetching the parameter values from a request/task:















Method: Get

Path: /attask/api/v9.0/optask

Params: action: generateApiKey

Param: apiKey: apikeyvalue

Param: ID: requestid

Param: fields:parameterValues


Similarly, if we need to fetch field values from project, then we need to replace ID with projectID, though end point will change.




Method: Get

Path: /attask/api/v9.0/project/search

Params: action: generateApiKey

Param: apiKey: apikeyvalue

Param: ID: projectID

Param: fields:parameterValues


Similarly, if you want to fetch the custom form names for a project, you can get it with below details:



Method: Get

Path: /attask/api/v9.0/PROJ/search

Params: action: generateApiKey

Param: apiKey: apikeyvalue

Param: ID: projectID

Param: fields:objectCategories:category:name


This will give all the custom form name for the project.



Sunday, 5 September 2021

September 05, 2021

[Zoom] How to create zoom calendar meetings via API

 If you are using zoom APIs and at some time, you need to create meetings via API's, then the below endpoints can help:

1. First create a token. Here, I am using OAuth2.0. To create Oauth2.0, we can use client id and secret id.


1. Grant Type: Authorization Code
2. Callback URL: https://oauth.pstmn.io/v1/callback
3. Authorize using a browser
4. Auth URL: https://zoom.us/oauth/authorize
5. Access Token URL: https://zoom.us/oauth/token
6. Client ID
7. Client Secret

2. Once the auth is created, it can be used in meetings API.

For Example:

To list all meetings, the following curl can be seen:

Get: https://api.zoom.us/v2/users/me/meetings

3. To create a new meeting, we can use the below API:


Body:

{
"topic": "Meting created via - API- ${{$randomInt}}",
"type": 2,
"start_time": "2021-09-10T12:10:10Z",
"duration": "3",
"settings": {
"host_video": true,
"participant_video": true,
"join_before_host": true,
"mute_upon_entry": "true",
"watermark": "true",
"audio": "voip",
"auto_recording": "cloud"
}
}

4. To update a meeting, the below API can used:

https://api.zoom.us/v2/meetings/78257544816

{
"topic": "Meting uppdated after creation via -API -> ${{$randomInt}}",
"type": 2,
"start_time": "2021-09-10T12:10:10Z",
"duration": "3",
"settings": {
"host_video": true,
"participant_video": true,
"join_before_host": true,
"mute_upon_entry": "true",
"watermark": "true",
"audio": "voip",
"auto_recording": "cloud"
}

}

5. To Delete a meeting, it can be deleted with the below api.

https://api.zoom.us/v2/meetings/73068576098