如何使用*热代码替换*来更正我的xpath不正确 [英] How to use *Hot Code Replacement* to correct my xpath of it is incorrect

查看:85
本文介绍了如何使用*热代码替换*来更正我的xpath不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium Java Eclipse Mars 编写测试.我决定使用热代码替换调试我的硒测试.我决定使用HCR的主要原因是进入定位器(例如:WebElement searchBox = driverChrome.findElement(By.xpath("bla bla"));)并尝试使用xpath来查看其是否有效,如果不行,请对其进行更改,将其保存并在不运行的情况下再次尝试整个测试.

I am using Selenium, Java and Eclipse Mars to write a test. I decided to use Hot Code Replacement to debug my selenium tests. The main reason that I decided to use HCR was to get to my locators (for example: WebElement searchBox = driverChrome.findElement(By.xpath("bla bla"));) and try the xpath to see if it works or not, and if not I change it, save it and try it again without running the whole test.

我有一个问题,当我更改某些内容并单击保存"时,它会显示:

I have this problem that when I change something and click on save it shows:

:

下面找到我的示例代码:

Below find my sample code:

public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "Chrome Drivers\\chromedriverWindows32");
        try {
            WebDriver driverChrome = new ChromeDriver();
            driverChrome.get("http://www.google.com/xhtml");
            driverChrome.manage().window().maximize();
            String name = "bla bla";
            driverChrome.findElement(By.name(name)).click();
            Thread.sleep(2000); // Let the user actually see something!
            driverChrome.quit();
        } catch (Exception e) {         
            e.printStackTrace();
        }
    }

推荐答案

更新了更多详细信息

热代码替换不适用于每种代码情况.考虑将XPath设置为字符串变量,然后在调试过程中修改该变量的值.

Hot code replacement won't work for every code situation. Consider setting the XPath as a string variable, then modify the value of that variable as part of your debugging.

例如:

@Test
public void changeExpression() throws Exception {
    WebDriver browser = new FirefoxDriver();
    browser.get("http://google.com");
    WebElement searchBtn = 
            browser.findElement(By.xpath("*//form//input[@value='Google Search']"));
    //do other stuff...
    browser.quit();
}

现在在WebElement searchBtn ...语句上设置一个断点.在调试模式下运行测试.跨过断点,以便执行browser.findElement语句.您将拥有一个初始化的WebElement searchBtn或一个异常,因为xpath无效.

Now set a breakpoint on the WebElement searchBtn ... statement. Run the test in Debug mode. Step over the breakpoint so the browser.findElement statement executes. You'll either have an initialized WebElement searchBtn or an exception because the xpath was invalid.

现在,您可以在Eclipse中打开表达式"窗口(窗口->显示视图=>其他=>表达式").

Now you can open up the Expressions window in Eclipse (Window -> Show View => Other => Expressions).

您可以在此处编辑find语句,特别是xpath语句.

You can edit the find statement here, specifically the xpath statement.

坦率地说,如果您只是想验证Xpath,那么在Chrome,IE或Firefox中使用开发工具会容易得多:

Frankly, if you're just trying to validate Xpaths it would be lots easier to use dev tools in Chrome, IE, or Firefox:

HTH

这篇关于如何使用*热代码替换*来更正我的xpath不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆