问题
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();
}
}
回答1:
Updated with more detail
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.
For example:
@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();
}
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.
Now you can open up the Expressions window in Eclipse (Window -> Show View => Other => Expressions).
You can edit the find statement here, specifically the xpath statement.
Frankly, if you're just trying to validate Xpaths it would be lots easier to use dev tools in Chrome, IE, or Firefox:
HTH
来源:https://stackoverflow.com/questions/36409978/how-to-use-hot-code-replacement-to-correct-my-xpath-of-it-is-incorrect