Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 我们能够在页面对象模型中打印对象和xpath吗_Selenium_Selenium Webdriver_Ui Automation - Fatal编程技术网

Selenium 我们能够在页面对象模型中打印对象和xpath吗

Selenium 我们能够在页面对象模型中打印对象和xpath吗,selenium,selenium-webdriver,ui-automation,Selenium,Selenium Webdriver,Ui Automation,我正在使用testng+java+maven使用POM。为了报告的目的,我们可以在logs/extendreport中按objectname打印吗? 我正在使用Log4j和extentreports 示例:Mycommonfunction类包含为测试用例执行的所有操作: public void click(By element) { WebElement webElement = getElement(element); try { Lo

我正在使用testng+java+maven使用POM。为了报告的目的,我们可以在logs/extendreport中按objectname打印吗? 我正在使用Log4j和extentreports

示例:Mycommonfunction类包含为测试用例执行的所有操作:

public void click(By element) {
        WebElement webElement = getElement(element);
        try {

            Log.info("Clicking on the webelement " + element);
            webElement.click();
            ExtentTestManager.getTest().info("clicking on the webelement - " + element);
        }

        catch (NoSuchElementException e) {
            Log.error(e.getMessage());
            ExtentTestManager.getTest().fail(e);
            throw new TestException(String.format("The following element is not clickable: [%s]", element));
        }
    }
在我的pageobject类中,我将xpath声明为:

By clearFormButton = By.xpath("//*[@id='createPopulation:j_idt198']");

and in my test step:
commonfunction.click(clearFormButton);

Actual output : Clicking on the webelement By.xpath: 
//*[@id='createPopulation:j_idt198']

Expected output : Clicking on the webelement clearFormButton - By.xpath: 
//*[@id='createPopulation:j_idt198']
e.extraInfo.get(“***元素信息”)
将为您提供定位器及其值

log.debug(e.extraInfo.get(“***元素信息”))

您可以使用获取类实例的字段值,如下所示:

java.lang.reflect.Field field = element.getClass().getDeclaredField("foundBy");
field.setAccessible(true);
String foundBy = field.get(element).toString();


您对的实现有点奇怪,顺便说一句,我的设想是您的测试不应该有任何Selenium内部构件,所以不要使用
commonfunction在测试步骤中,您应该有类似于
CreatePopulationPage=myCurrentPage的内容。单击createpopulationbutton()

按类型变量名“element”将在代码中打印在哪里?问题要求在log.info或extent report log中按变量名打印以获取信息,但这里的答案仅适用于异常发生时的情况??请,请,当您仅通过ID定位元素时,请不要使用XPath。使用
By.ID()
取而代之。你真的想在成功点击的海洋中寻找不起作用的点击吗?不要记录单击,而是学习如何在堆栈跟踪失败并抛出错误时读取堆栈跟踪。堆栈跟踪将准确地告诉您脚本在哪一行失败,以及定位器信息。
java.lang.reflect.Field field = element.getClass().getDeclaredField("foundBy");
field.setAccessible(true);
String foundBy = field.get(element).toString();