how to get the xpath from Katalon studio's testObject in 3rd party jar file

user3302083 :

I want to create 3rd party jar file for "customize wait" functionality, which will further use in Katalon studio Test Cases to synchronize the test case by calling "waitTillObjectPresent()".

Sample of my calling function from Katalon Studio would be like below:
Here I am trying to call java function "waitTillObjectPresent()" from Katalon Studio.

  WebUI.navigateToUrl('https://www.companysite.com/en')
//Wait for element till present upto 10 sec.
        WaitForObject.waitTillObjectPresent'(findTestObject('V3-Web/WaitForObject/Page_Livtten/button_Results'), 10)
        WebUI.click(findTestObject('V3-Web/WaitForObjectDemo/Pagetten/button_Results'))

Note : In the above code "WaitForObject" is my Java Class and "waitTillObjectPresent" is fluent wait method in same class.

Tried with following java code :

public void waitTillObjectPresent(TestObject to, int waitingtime){
// HERE IS ISSUE, I am not getting TestObject from Katalon Studio calling method 

   int counter=0;
   String locator= object.findPropertyValue('xpath');

    System.out.println("xpath is:: " + locator);
    WebDriver driver = DriverFactory.getWebDriver();
  // HERE IS ISSUE, I am not able to getting  WebDriver instance from Katalon Studio 

    // fluent wait method 
            Wait wait = new FluentWait(driver )
            .withTimeout(waitingtime, TimeUnit.SECONDS)
            .pollingEvery(1000, TimeUnit.MILLISECONDS)
            .ignoring(WebElementNotFoundException.class)

            WebElement ele = (WebElement) wait.until(new Function<WebDriver, WebElement>()  {
                public WebElement apply(WebDriver driver) {
                    counter ++
                    return driver.findElement(By.xpath(locator));
                }
            });
    System.out.println("Waiting time for Object ::: "+ object+" ::: rendering is :::: " +counter*700 +"  ::: miliseconds ie in seconds ::: " +(counter*700)/1000);
    }
}

In the above code I am getting errors at 2 points:

1st ISSUE : I am not getting the TestObject in java program from Katalon studio.

2nd ISSUE : I am not able to get the webdriver instance with the code WebDriver driver = DriverFactory.getWebDriver();

Kindly help me I am new to Katalon studio.

user1999758 :

Here is best solution:

1st ISSUE : I am not getting the TestObject in java program from Katalon studio.

For the above issue , you can not collect the value of object like this String locator= object.findPropertyValue('xpath'); you need to do something like this to get the values from object

public static String  getFieldNamesAndValues(final Object obj, boolean publicOnly)
            throws IllegalArgumentException, IllegalAccessException {
        Class<? extends Object> c1 = obj.getClass();
        System.out.println("Class value is c1:::" + c1);
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = c1.getDeclaredFields();
        System.out.println("Fields in objects :: " + fields.toString());
        System.out.println("Xpath Before for loop::: " + fields.getClass());
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            System.out.println("Fields name ::: " + name);
            if (publicOnly) {
                if (Modifier.isPublic(fields[i].getModifiers())) {
                    Object value = fields[i].get(obj);
                    map.put(name, value);
                }
            } else {
                fields[i].setAccessible(true);
                Object value = fields[i].get(obj);
                map.put(name, value);
            }
        }
        System.out.println("Return on object is ::::--> " + map.get("selectorCollection").toString());
        return (String) map.get("selectorCollection");
    }

And call the above program getFieldNamesAndValues(Testobject, false)instead of do this String locator= object.findPropertyValue('xpath');

2nd ISSUE : I am not able to get the webdriver instance with the code WebDriver driver = DriverFactory.getWebDriver();

For the issue 2nd : pass the DriverFactory.getWebDriver() instance from Katalon studio to java like this

//Wait for element till present upto 10 sec.
        WaitForObject.waitTillObjectPresent'(DriverFactory.getWebDriver() , findTestObject('V3-Web/WaitForObject/Page_Livtten/button_Results'), 10)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323499&siteId=1