我想创建回归测试,但每个版本升级都会生成新的随机ID。因此在新版本中失败了所有测试用例。例如:
版本1.1
driver.findElement(By.id("baseInputItems:0:j_idt2468_label")).click();
版本1.2
driver.findElement(By.id("baseInputItems:0:j_idt875_label")).click();
如何使用Java在Selenium WebDriver中解决此问题?将不胜感激任何帮助。
HTML代码中的两个元素:
<input type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" onchange="PrimeFaces.ab({s:"componentIndicators:0:j_idt2989:text_bldata",e:"valueChange",f:"newApplicationForm",p:"componentIndicators:0:j_idt2989:text_bldata",ps:true});" size="10" maxlength="100" value="1" name="componentIndicators:0:j_idt2989:text_bldata" id="componentIndicators:0:j_idt2989:text_bldata" role="textbox" aria-disabled="false" aria-readonly="false">
<input type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" style="text-align:left;" onchange="PrimeFaces.ab({s:"componentIndicators:1:j_idt2989:text_blyear",e:"valueChange",f:"newApplicationForm",p:"componentIndicators:1:j_idt2989:text_blyear",ps:true});" size="10" maxlength="10" value="1" name="componentIndicators:1:j_idt2989:text_blyear" id="componentIndicators:1:j_idt2989:text_blyear" role="textbox" aria-disabled="false" aria-readonly="false">
答案 0 :(得分:1)
看来你的输入元素可以通过id:
的这一部分来识别componentIndicators:0:
componentIndicators:1:
因为你提到的输入元素有&#34; 0&#34;,另一个&#34; 1&#34;
如果确实如此,您可以通过xpath搜索:
WebElement elementWithIdZero = driver.findElement(By.xpath("//input[contains(@id, 'componentIndicators:0:')]"));
WebElement elementWithIdOne = driver.findElement(By.xpath("//input[contains(@id, 'componentIndicators:1:')]"));