我正在尝试在这些网站上自动创建用户。
创建用户时,我获得了新员工的ID号,我将这些ID放在了员工表中的元素内。
但是它说该元素不在此处建立。
但是看起来正确,但是也许我错过了什么?有人可以让我知道我在这里缺少什么。我的功能:
public void IDCompare() {
waitVisibility(empIDField);
employeeID = getAttribute(empIDField, IDattribute);
moveToElement(pimTab, employeeListTab);
waitVisibility(employeeIDINTable);
Assert.assertEquals ((getText(employeeIDINTable)), employeeID);
}
表中的元素是:
By employeeIDINTable = By.xpath("(//tr[contains(.,"+employeeID+")]/td/a)[1])");
我从员工那里得到的属性是:
String IDattribute = "value";
我从TestNG得到的错误是:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: (//tr[contains(.,0)]/td/a)[1]) (tried for 10 second(s) with 500 MILLISECONDS interval)
答案 0 :(得分:1)
我的代码可以找到所有东西,
定位器为By employeeIDINTable = By.xpath("//*[contains(text(),"+employeeID+")]");
唯一的事情是我需要再次输入By employeeIDINTable = By.xpath("//*[contains(text(),"+employeeID+")]");
,因为employeeID
仅在方法中初始化。
这些是解决方案
答案 1 :(得分:0)
我认为您在使用错误的定位器来标识您创建的员工ID。
尝试使用下面的xPath,它将从表中标识传递的员工ID行:
//a[contains(text(), 'pass employee id here')]
例如如下所示:
//a[contains(text(), '0017')]
假设您要像下面一样提取和存储actual employee id
String actualEmployeeID = driver.findElement(By.id("employeeId")).getAttribute("value").trim();
然后尝试稍微修改您的IDCompare()
函数,以使其易于阅读和理解,请在此处应用上述xpath:
public void idCompare() throws Exception {
// Wait for sometime
Thread.sleep(2000);
// Get expected employee id from the table
String expectedEmployeeID = driver.findElement(By.xpath("//a[contains(text(), '"+actualEmployeeID+"')]")).getText().trim();
// Compare actual and the expected values
Assert.assertEquals(actualEmployeeID, expectedEmployeeID);
}
如果您仍然发现困难,那么下面是整个代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import com.google.common.base.Function;
public class EmployeeID {
private static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\NotBackedUp\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("Your site name");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(3, TimeUnit.MINUTES).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class, StaleElementReferenceException.class);
WebElement username = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("txtUsername"));
}
});
WebElement password = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("txtPassword"));
}
});
WebElement submit = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("btnLogin"));
}
});
// Enter your site user name
username.sendKeys("Enter Username here");
// Enter your site password
password.sendKeys("Enter Password here");
submit.click();
WebElement pim = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("menu_pim_viewPimModule"));
}
});
new Actions(driver).moveToElement(pim).build().perform();
WebElement addEmployee = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("menu_pim_addEmployee"));
}
});
addEmployee.click();
WebElement firstName = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("firstName"));
}
});
WebElement middleName = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("middleName"));
}
});
WebElement lastName = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("lastName"));
}
});
firstName.sendKeys("Arya");
middleName.sendKeys("Stark");
lastName.sendKeys("Starky");
WebElement getEmployeeID = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("employeeId"));
}
});
String employeeID = getEmployeeID.getAttribute("value");
WebElement save = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("btnSave"));
}
});
save.click();
WebElement employeeList = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("menu_pim_viewEmployeeList"));
}
});
employeeList.click();
WebElement newlyCreatedEmployeeInTable = wait.until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("//a[text()='"+employeeID+"']"));
}
});
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", newlyCreatedEmployeeInTable);
System.out.println(employeeID+" is in the Table ? "+newlyCreatedEmployeeInTable.getText().trim().equals(employeeID));
}
}
希望对您有帮助...