Skip to main content

As the code is similar, they can be merged together.

I'll suggest you to break down the functions into smaller reusable chunks which will follow the Single ResponsibleResponsibility Principle  (SRP). These functions will be testable and reusable.

Both functions are

  1. Adding a delay of 500 milliseconds
  2. Selecting elements from provided page
  3. Clicking the first element from the collection
  4. Finding an element from it'sits ancestor and returning the innerText of it.

As, these are the steps, we can similarly divide those functions into smaller functions each doing the task in eachof one step above.

For first step- (to add delay,) I've not created a new function as I guess that is already created in utils module.


The first function  (Step #2) will select the elements from the page using the provided xpath and return the first of them.

function getFirstVisiblePublication(page, xpath) {
    aqUtils.Delay(500);

    var elements = page.EvaluateXpath(xpath);
    return elements[0];
}

The second function(Step combines Steps #3 & #4), again#4; as #3 only requirerequires to click the element I haven't created new function.

The below function will accept the page in which the target element is to be searched and xpath of the element. This will call the above function to get the first element and click it.

This function will return the text/label of the anchor element which is getobtained by using the first element's ancestors.

function getFirstVisiblePublicationAnchorLabel(page, xpath) {
    // Get first element
    var firstElement = getFirstVisiblePublication(page, xpath);
    firstElement.Click();

    return firstElement.parent.parent.FindChildByXPath('//a').textContent;
}

Usage:

Function ##Usage: Function can be called as followfollows:

var hideButtonXpath = "//img[@title='To hide this item in your profile, mark it as invisible']";
var publicationTitle = getFirstVisiblePublicationAnchorLabel(PageObj, hideButtonXpath);

As the code is similar, they can be merged together.

I'll suggest you to break down the functions into smaller reusable chunks which will follow the Single Responsible Principle(SRP). These functions will be testable and reusable.

Both functions are

  1. Adding a delay of 500 milliseconds
  2. Selecting elements from provided page
  3. Clicking the first element from the collection
  4. Finding an element from it's ancestor and returning the innerText of it.

As, these are the steps, we can similarly divide those functions into smaller functions each doing the task in each step above.

For first step-to add delay, I've not created a new function as I guess that is already created in utils module.


The first function(Step #2) will select the elements from the page using the provided xpath and return the first of them.

function getFirstVisiblePublication(page, xpath) {
    aqUtils.Delay(500);

    var elements = page.EvaluateXpath(xpath);
    return elements[0];
}

The second function(Step #3 & #4), again as #3 only require to click the element I haven't created new function.

The below function will accept the page in which the target element is to be searched and xpath of the element. This will call the above function to get the first element and click it.

This function will return the text/label of the anchor element which is get by using the first element's ancestors.

function getFirstVisiblePublicationAnchorLabel(page, xpath) {
    // Get first element
    var firstElement = getFirstVisiblePublication(page, xpath);
    firstElement.Click();

    return firstElement.parent.parent.FindChildByXPath('//a').textContent;
}

Usage:

Function can be called as follow:

var hideButtonXpath = "//img[@title='To hide this item in your profile, mark it as invisible']";
var publicationTitle = getFirstVisiblePublicationAnchorLabel(PageObj, hideButtonXpath);

As the code is similar, they can be merged together.

I'll suggest you to break down the functions into smaller reusable chunks which will follow the Single Responsibility Principle  (SRP). These functions will be testable and reusable.

Both functions are

  1. Adding a delay of 500 milliseconds
  2. Selecting elements from provided page
  3. Clicking the first element from the collection
  4. Finding an element from its ancestor and returning the innerText of it.

As these are the steps, we can similarly divide those functions into smaller functions each doing the task of one step above.

For first step (to add delay) I've not created a new function as I guess that is already created in utils module.


The first function  (Step #2) will select the elements from the page using the provided xpath and return the first of them.

function getFirstVisiblePublication(page, xpath) {
    aqUtils.Delay(500);

    var elements = page.EvaluateXpath(xpath);
    return elements[0];
}

The second function combines Steps #3 & #4; as #3 only requires to click the element I haven't created new function.

The below function will accept the page in which the target element is to be searched and xpath of the element. This will call the above function to get the first element and click it.

This function will return the text/label of the anchor element which is obtained by using the first element's ancestors.

function getFirstVisiblePublicationAnchorLabel(page, xpath) {
    // Get first element
    var firstElement = getFirstVisiblePublication(page, xpath);
    firstElement.Click();

    return firstElement.parent.parent.FindChildByXPath('//a').textContent;
}

##Usage: Function can be called as follows:

var hideButtonXpath = "//img[@title='To hide this item in your profile, mark it as invisible']";
var publicationTitle = getFirstVisiblePublicationAnchorLabel(PageObj, hideButtonXpath);
Source Link
Tushar
  • 3k
  • 1
  • 21
  • 28

As the code is similar, they can be merged together.

I'll suggest you to break down the functions into smaller reusable chunks which will follow the Single Responsible Principle(SRP). These functions will be testable and reusable.

Both functions are

  1. Adding a delay of 500 milliseconds
  2. Selecting elements from provided page
  3. Clicking the first element from the collection
  4. Finding an element from it's ancestor and returning the innerText of it.

As, these are the steps, we can similarly divide those functions into smaller functions each doing the task in each step above.

For first step-to add delay, I've not created a new function as I guess that is already created in utils module.


The first function(Step #2) will select the elements from the page using the provided xpath and return the first of them.

function getFirstVisiblePublication(page, xpath) {
    aqUtils.Delay(500);

    var elements = page.EvaluateXpath(xpath);
    return elements[0];
}

The second function(Step #3 & #4), again as #3 only require to click the element I haven't created new function.

The below function will accept the page in which the target element is to be searched and xpath of the element. This will call the above function to get the first element and click it.

This function will return the text/label of the anchor element which is get by using the first element's ancestors.

function getFirstVisiblePublicationAnchorLabel(page, xpath) {
    // Get first element
    var firstElement = getFirstVisiblePublication(page, xpath);
    firstElement.Click();

    return firstElement.parent.parent.FindChildByXPath('//a').textContent;
}

Usage:

Function can be called as follow:

var hideButtonXpath = "//img[@title='To hide this item in your profile, mark it as invisible']";
var publicationTitle = getFirstVisiblePublicationAnchorLabel(PageObj, hideButtonXpath);