我想为“定制等待”功能创建第3方jar文件,该文件将在Katalon Studio测试用例中进一步使用,以通过调用“waitTillObjectPresent()”来同步测试用例。

我从Katalon Studio调用函数的示例如下:
在这里,我试图从Katalon Studio调用Java函数“waitTillObjectPresent()”。

  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'))

注意:在上面的代码中,“WaitForObject”是我的Java类,而“waitTillObjectPresent”是同一类中的流畅的等待方法。

尝试使用以下Java代码:
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);
    }
}

在上面的代码中,我在两点出现错误:

第一个问题:我没有从Katalon studio的Java程序中获得TestObject。

第二个问题:我无法使用代码WebDriver driver = DriverFactory.getWebDriver();获得webdriver实例

请帮助我,我是Katalon工作室的新手。

最佳答案

这是最好的解决方案:

第一个问题:我没有从Katalon studio的Java程序中获得TestObject。

对于上述问题,您无法像String locator= object.findPropertyValue('xpath');这样收集对象的值,您需要执行类似的操作才能从对象中获取值

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");
    }

并调用上面的程序getFieldNamesAndValues(Testobject, false)而不是执行此String locator= object.findPropertyValue('xpath');
第二个问题:我无法使用代码WebDriver driver = DriverFactory.getWebDriver();来获取Webdriver实例。

对于第二个问题:将Katalon Studio中的DriverFactory.getWebDriver()实例传递给Java,如下所示
//Wait for element till present upto 10 sec.
        WaitForObject.waitTillObjectPresent'(DriverFactory.getWebDriver() , findTestObject('V3-Web/WaitForObject/Page_Livtten/button_Results'), 10)

关于java - 如何在第3方jar文件中从Katalon工作室的testObject获取xpath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58594786/

10-12 02:49