如何在范围报告中打印 WebElement 变量名称?

最终目标是获取用于报告目的的 WebElement 变量名称。


下面是我的代码。


A类:


Class A{


      public void Click(WebElement element)

      {

            element.click();

            System.out.println("Clicked on"+ element);

      }

}

B类:


class B extends A{


     @FindBy(xpath = "//li[@class='login-menu']//a[text()='Log In']")

     WebElement link_Log_In;


     Click(link_Log_In);

}

期望的输出:


点击link_Log_In


实际输出:


点击[[ChromeDriver: chrome on XP (acc46d4d382511d7b18396d4a4dddd30)] -> xpath: //li[@class='login-menu']//a[text()='Log In']]


另外,我想在 WebDriverEventListener 中使用 afterClickOn(WebElement element, WebDriver driver) 方法在范围报告中打印相同的Desired Output 。


我以这样一种方式创建了框架,即 WebDriverEventListener 结果根据需要打印在范围报告中,但无法如上所述打印所需的输出。


请告知我是否在上面的代码中遗漏了什么,并在 WebDriverEventListener 的 afterClickOn(WebElement element, WebDriver driver) 方法中实现相同的功能


下面是事件监听器 afterClick 方法


public void afterClickOn(WebElement element, WebDriver driver) {

        System.out.println("Clicked On"+element); // this is to print in console

        ExtentManager.getTest().log(Status.valueOf(element), "clicked on"); // this is to print in extent report

    }


眼眸繁星
浏览 62回答 2
2回答

隔江千里

老问题,但对于那些会遇到同样问题的人。一种可能的解决方案是扩展ElementLocator(例如DefaultElementLocator)您将在findBy()方法中记录元素名称的位置。喜欢:public class WECElementLocator extends DefaultElementLocator {    final String UNNAMED = UUID.randomUUID().toString();    final String elementName;    public WECElementLocator(SearchContext searchContext, Field field) {        super(searchContext, field);        Name elementNameAnnotated = field.getAnnotation(Name.class);        if (elementNameAnnotated != null){            elementName = elementNameAnnotated.value();        }else{            elementName = UNNAMED;        }    }    private void log(String message){        if(!UNNAMED.equals(elementName)){            System.out.println(message + " (for [" + elementName + "])");        }    }    @Override    public WebElement findElement() {        try{            log("Attempt to lookup element..");            WebElement result = super.findElement();            log("Element successfully located.");            return result;        }catch (Throwable e){            log("Problem in locating element..");            throw e;        }    }}您可以引入自己的注释,该注释将为您的字段分配一个人类可读的名称。喜欢:public class PageObjectWithName {    @Name("My test name")    @FindBy(xpath = "//input[@type='button']")    WebElement button;    public PageObjectWithName(SearchContext context) {        // TODO: implement page initialization    }    public void clickButton(){        button.click();    }}因此,在初始化部分,您将只使用元素定位器的自定义实现,它将获取注释并使用字段中的名称。不需要额外的反射工作。这将与PageFactory线束顺利集成。

达令说

我不相信有一种简单的方法可以获取您为元素提供的名称(例如 getName() 方法)。我能想到的最接近的事情是执行以下操作:public class ElementNaming {&nbsp; &nbsp; //Create a hashmap that stores the name of the element as a string&nbsp; &nbsp; public static HashMap<WebElement, String> webElementKeys = new HashMap<>();&nbsp; &nbsp; public static By by = By.id("hplogo");&nbsp; &nbsp; public static WebElement instantiateWebElementAndName(String name, By by) {&nbsp; &nbsp; &nbsp; &nbsp; webElementKeys.put(driver.findElement(by), name);&nbsp; &nbsp; &nbsp; &nbsp; return driver.findElement(by);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[]args) {&nbsp; &nbsp; &nbsp; &nbsp; driver.get("https://www.google.com/");&nbsp; &nbsp; &nbsp; &nbsp; WebElement element = instantiateWebElementAndName("element", by);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Element name: " + webElementKeys.get(element));&nbsp; &nbsp; }}Console Output:&nbsp;Element name: element
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java