selenium webdriver - 构建动作click和sendkeys

时间:2018-02-21 07:57:44

标签: java selenium constructor

我有以下几件:

WebElement L3_Y1_total_x = driver.findElement(By.xpath("//input[contains(@class,'sumCell blahblah total')]"));

String L3_Y1_total_x1_value = L3_Y1_total_x.getAttribute("value");
WebElement L3_C1 = driver.findElement(By.xpath("//input[contains(@class,'cell blahblah1 decimal')]"));
L3_C1.click();L3_C1.sendKeys("3,00");L3_C1.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y1_total_x, "value", L3_Y1_total_x1_value));} catch (TimeoutException e) {};

String L3_Y2_total_x1_value = L3_Y2_total_x.getAttribute("value");
WebElement L3_C2 = driver.findElement(By.xpath("//input[contains(@class,'cell cell blahblah2 decimal')]"));
L3_C2.click();L3_C2.sendKeys("3,00");L3_C2.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y2_total_x, "value", L3_Y2_total_x1_value));} catch (TimeoutException e) {};

String L3_Y3_total_x1_value = L3_Y3_total_x.getAttribute("value");
WebElement L3_C3 = driver.findElement(By.xpath("//input[contains(@class,'cell blahblah3 decimal')]"));
L3_C3.click();L3_C3.sendKeys("3,00");L3_C3.sendKeys(Keys.TAB);
try {wait10s.until(Attribute_Not_To_Be.attributeNotToBe(L3_Y3_total_x, "value", L3_Y3_total_x1_value));} catch (TimeoutException e) {};

如何缩小代码?至少是element.click();element.sendKeys("xy");...序列。

感谢

1 个答案:

答案 0 :(得分:1)

这样的东西应该适合你的目的(虽然我现在不能尝试)。使用L3_Y1_total_xL3_Y2_total_xL3_Y3_total_x以及相应的参数blahblah来调用该函数。

public void doAction(WebElement elem, String blahblah) {
    String value = elem.getAttribute("value");
    WebElement L3_C1 = driver.findElement(By.xpath("//input[contains(@class,'cell " + blablah + "decimal')]"));
    L3_C1.click();
    L3_C1.sendKeys("3,00");
    L3_C1.sendKeys(Keys.TAB);
    try {
        wait10s.until(Attribute_Not_To_Be.attributeNotToBe(elem, "value", value));
    } catch (TimeoutException e) {};
}

请注意,使用下划线和大写变量会违反广泛接受的Java编码约定。使用驼峰套管和小写变量。