How to use *Hot Code Replacement* to correct my xpath of it is incorrect

时间:2016-04-04 18:19:04

标签: java debugging selenium selenium-webdriver selenium-chromedriver

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:

enter image description here:

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 个答案:

答案 0 :(得分:0)

更新了更多详情

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

例如:

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("StationInfo.txt");

    try (Scanner sc = new Scanner(file)) {
        while (sc.hasNext()) {
            String stationTab = sc.next();
            // Looking for tag 'Station:'
            if (!stationTab.equals("Station:")) continue;

            if (!sc.hasNext()) {
                break;
            }

            String station = sc.next();
            if (!sc.hasNextInt()) {
                continue;
            }
            int x = sc.nextInt();
            if (!sc.hasNextInt()) {
                continue;
            }
            int y = sc.nextInt();
            System.out.println(station + " " + x + " " + y);
        }

    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    }
}

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

现在您可以在Eclipse中打开Expressions窗口(Window - > Show View => Other => Expressions)。

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

warns against that

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

enter image description here

HTH