我收到一張表並能夠獲取並驗證數據(電子郵件是否爲電子郵件等等)。我們要驗證前端顯示的數據與後端。有一張桌子(我看過桌子 - 第一列是名字,然後是電子郵件,電話號碼,公司,國家和日期)。Selenium - 更新表格列(如果已更改)
現在,在前端的人,切換了列。我之前看過桌子,所以我知道我會收到我的信息的順序。每次在前端進行小改動時,我都必須更改代碼。僅供參考,表頭是用「數據名稱」定義的,所以如果你的解決方案涉及到表格頭部的話,我將能夠使用它。我的代碼下面貼:
public static void getTableContents(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
//TODO Replace this w. until it find command
e.printStackTrace();
}
log("TABLE CONTENTS VERIFICATION");
//Get the table contents
WebElement table_element = driver.findElement(By.className(TABLE_RESPONSE));
//Click on a button to switch into the desired column
WebElement switchColumn = driver.findElement(By.cssSelector(".switchColumn img.pull-right"));
switchColumn.click();
// We do not want the first two contents as they represent default or error case.
List<WebElement> tr_collection=table_element.findElements(By.xpath("//tbody//tr[position()>2]"));
int i=1;
for(WebElement trElement : tr_collection)
{
List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
int j=1;
for(WebElement tdElement : td_collection)
{
String check = tdElement.getText();
if(j==1) {
if(isValidName(check))
passed("Name Test : ");
else{
log(check + " is not a valid name");
}
}
if(j==2) {
if(isValidEmail(check))
{ passed("Email address Test : ");
}
else
log(check + " is not a valid email");
}
if(j==3) {
if(isValidNumber(check))
passed("Valid Number test : ");
else
log(check + " is not a valid number");
}
if(j==6){
if(isValidIccid(check))
passed("Valid Iccid test : ");
else
log(check+ " is not a valid Iccid");
}
if(j==4){
//Blank (for a while)
}
if(j==5){
if(isValidCountry(check))
passed("Valid country test : ");
else
log(check+ " is not a valid country");
}
j++;
}
System.out.println();
i++;
}
}
有沒有改變此代碼,以我的要求的任何快捷方式?我無法一直改變數字(1,2,3,4,5,6)。我只是在尋找巧妙的方法來更新我的代碼,而不是完全改變我的代碼。我不知道如何解決這個問題。EDIT:另外,我將改變我的if/else語句來切換案例,以便更容易理解。但截至目前,我遇到了一個很大的問題,因爲我可能不得不更改我的整個代碼,所以請不要介意。