如何在字段中重置用户名?

时间:2014-08-31 08:03:24

标签: java selenium selenium-webdriver thread-sleep

有一个用户名表,我从这些表中编辑了用户名“Pra”到“Pra1”,但现在当我来到用户表列表时,它找不到名为“Pra”的用户,因为它更新为“Pra1”。 所以,我想要的是在更新搜索过滤器中的用户名后,用更新的用户名搜索(使用“Pra1”)。 以下是更新后的用户名的运行代码,请在此之后帮助我:

public class InsSystemCenter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver = new FirefoxDriver();
        driver.get("URL");

        driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);

        // Verify the insfocus system center page has been opened or not by
        // xpath//

        if (driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td/h1")) != null) {
            System.out.println("Center has been opened");
        } else {
            System.out.println("Center is not displaying");
        }

        WebElement userName = driver.findElement(By.name("username"));
        userName.sendKeys("pra");

        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("123456");

        WebElement signIn = driver
                .findElement(By
                        .xpath("/html/body/table/tbody/tr[2]/td/table/tbody/tr[1]/td/form/center/div/table/tbody/tr[4]/td/input"));
        signIn.click();

        driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);

        // Verify that welcome page is displayed or not//

        WebElement welcomePageVerify = driver
                .findElement(By
                        .xpath("/html/body/table/tbody/tr[3]/td/table/tbody/tr[1]/td/div[2]/span"));
        if (welcomePageVerify.isDisplayed()) {
            System.out.println("Welcome page is displaying");
        } else {
            System.out.println("Welcome page is not displaying");
        }

        // Try to click on the tab name "Settings" by xpath//

        WebElement settings = driver.findElement(By
                .xpath("/html/body/table/tbody/tr[2]/td/ul/li[3]/a"));
        settings.click();

        // Verifying that after clicking on "Setting" it opens a database page
        // or not//

        WebElement settingsVerify = driver
                .findElement(By
                        .xpath("/html/body/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td[2]/h1/span"));
        if (settingsVerify.isDisplayed()) {
            System.out
                    .println("Database page is displayed after clicking on Setting tab");
        } else {
            System.out.println("Database page is not displaying");
        }

        driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);

        // Click on the button "Users" by xpath, so that it goes to the page
        // where it show the list of users //

        WebElement users = driver
                .findElement(By
                        .xpath("/html/body/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[1]/p/a[2]"));
        users.click();

        // Verifying for users page opened or not//

        WebElement usersPageVerify = driver
                .findElement(By
                        .xpath("/html/body/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/div/table/tbody/tr/td[2]/h1"));
        if (usersPageVerify.isDisplayed()) {
            System.out
                    .println("Users page is displayed after clicking on users button");
        } else {
            System.out.println("Users page is not displaying");
        }

        driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);


        System.out.println("Total time take " + new Date());
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            System.out.println("Error in wait");
        }
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        System.out.println("Total time take " + new Date());

        WebElement usUserName = driver.findElement(By.id("g_UserName"));
        usUserName.sendKeys("Pra");
        usUserName.sendKeys(Keys.ENTER);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println("Error in wait");
        }
        usUserName.sendKeys(Keys.TAB);
        System.out.println("Total time take " + new Date());
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            System.out.println("Error in wait");
        }


        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);

        // Checked the checkbox of find elemet or user//
        WebElement checkbox = driver.findElement(By.id("jqg_tblMainTable_243"));
        checkbox.click();

        // After checked the checkbox it enables the edit button for edit the
        // user//
        WebElement editButton = driver.findElement(By.id("btnEdit"));
        editButton.click();

        // ------------Edit user popup--------------//

        // Verify edit popup opened or not//
        String source = driver.getPageSource();
        int a = source.indexOf("Edit User:");
        if (a > 0) {
            System.out.println("Edit user popup is displayed");
        } else {
            System.out.println("Edit user popup is not displaying");
        }

        // All the WebElement parameter is located here//

        WebElement euUserName = driver.findElement(By.id("UserName"));
        euUserName.clear();
        euUserName.sendKeys("pra1");
        WebElement euFullName = driver.findElement(By.id("txtFullName"));
        euFullName.clear();
        euFullName.sendKeys("pra1");
        WebElement euPassword = driver.findElement(By.id("txtPassword"));
        euPassword.sendKeys("123456");
        WebElement euConfirmPassword = driver.findElement(By
                .id("txtConfirmPassword"));
        euConfirmPassword.sendKeys("123456");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            System.out.println("Error in wait");
        }

        WebElement dropdown = driver.findElement(By.id("RoleID"));

        // Verify dropdown is displayed or not not //

        if (dropdown.findElement(By.id("RoleID")) != null) {
            System.out.println("Dropdown is displayed");
        } else {
            System.out.println("Dropdown is not displaying");
        }
        Select clickThis = new Select(dropdown);
        clickThis.selectByVisibleText("Normal");

        System.out.println("Drop down values "+clickThis.getOptions().get(0).getText());


        WebElement euUserDetail = driver.findElement(By.id("UserDetails"));
        euUserDetail.clear();
        euUserDetail.sendKeys("pra1");

        WebElement euOk= driver.findElement(By.xpath("/html/body/div[3]/div[3]/div/button[1]"));
        euOk.click();

        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
        driver.quit();
    }
}

3 个答案:

答案 0 :(得分:3)

在致电clear()之前,请致电sendKeys("your text")方法。

这应该有效。

答案 1 :(得分:0)

在将值传递给webelement

之前,只需使用clear函数
WebElement userName = driver.findElement(By.name("username"));

<强> userName.clear();

userName.sendKeys("pra");

您可以使用所有元素的相同方式。

答案 2 :(得分:-1)

好吧,我甚至不敢试图阅读一个700万行长的主要方法

1)重构它以分离逻辑方法(责任驱动设计)

2)当你完成后,我们会向你发布负责你的问题的代码

3)或许可以查看将要搜索的值分配给临时变量,以便再次找到它?