【问题标题】:How to Set up POM Structure using selenium java and (TestNG Framework)如何使用 selenium java 和(TestNG 框架)设置 POM 结构
【发布时间】:2017-04-26 04:50:12
【问题描述】:

我是 QA 自动化的新手,我对 Java 的知识一般, 所以我决定使用(Selenium+Java)来做自动化。 我会将我所做的代码附加到自动化中。脚本运行顺利。 但是我做的结构不正确,因为我想遵循 (POM-Selenium).POM-Page Oriented Model

此脚本导航如下

Login(Page)-->Personal(Drop/down Selection)--->AddEdit((Drop/down Selection))-->Personal(Page)-->Add(Button)-->PersonalDetails(Page )

测试场景是……

用户应该“登录”到系统,并且必须点击导航选项卡中的“个人”下拉菜单,然后会有一个“添加编辑”下拉选择。然后用户被引导到标题为“个人”的页面,用户应单击该页面上的“添加”按钮以转到另一个称为“个人详细信息”的页面,用户可以添加从页面提供的相关字段以添加新客户。

请帮我把它安排到 POM 结构中。因为我很难去想它是怎么回事。让我感到沮丧的是,为了让用户添加相关记录,他/她应该登录到系统并在 POM 中,它说登录是一个单独的页面,我必须浏览三个页面才能完成任务。如果你能帮助我,那就太好了。如果我的英语不好,它可以讨论,很抱歉。在自动化方面,请把我当作一个完全的菜鸟:) 谢谢!

这是我写的测试脚本...

package TestNG;

import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.openqa.selenium.*;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

//public String baseUrl;
//public  String driverPath;
//public WebDriver driver;

public class Tester {
    public String baseUrl = "http://xxx.xxx.xxx.xxx:xxxx/xxx/";
    public String driverPath = "C:\\Users\\User\\Desktop\\geckodriver.exe";
    public WebDriver driver;

    @Test(priority = 0)
    public void Login() {

        System.out.println("launching firefox browser");
        System.setProperty("webdriver.gecko.driver", driverPath);
        driver = new FirefoxDriver();
        driver.get(baseUrl);
        WebElement username = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"userId\"]")));
        username.sendKeys("xxxxxxxxx");
        WebElement password = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"loginPassword\"]")));
        password.sendKeys("xxxxxxxxxxxxxxxxxx");
        WebElement button = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                .presenceOfElementLocated(By.xpath("/html/body/div/div[3]/div/div/form/div[3]/div[2]/div/button")));
        button.click();
        String expectedTitle = "xxxxxxxxxxxxxxxxxxxxxxxx";
        String actualTitle = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[2]/div/div/h1")))
                .getText().toString();
        // System.out.println(actualTitle);
        Assert.assertEquals(actualTitle, expectedTitle);

        // driver.close();

    }

    @Test(priority = 1)
    public void Personal_Tab_Selection() {

        clickWhenReady("/html/body/div[2]/div[2]/nav/div/div[2]/div/div[1]/ul/li[5]/a", 10, driver);

    }

    @Test(priority = 2)
    public void Add_Edit_Selection() {

        clickLinkByHref("/rsa/5/15/staff/n/i/list", driver);

    }

    @Test(priority = 3)
    public void Add_Button() {
        clickWhenReady("/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/a", 10, driver);

    }

    @Test(priority = 4)
    public void Radio_Button_AMW() {
        WebElement amw_radio = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[1]/div/div/label[1]")));
        amw_radio.click();
    }

    @Test(priority = 5)
    public void Radio_Button_service_provider() {
        WebElement service_provider = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[2]/div/div/label[1]")));
        service_provider.click();

    }

    @Test(priority = 6)
    public void Service_Provider_Name_select() {

        WebElement Service_Provider_DD = (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(
                        By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[3]/div/div/a")));
        Service_Provider_DD.click();

        driver.findElement(By.cssSelector("ul > li:nth-child(2)")).click();

    }

    @Test(priority = 7)
    public void Employee_Code_Enter() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[5]/div/input"))
                .sendKeys("01112");

    }

    @Test(priority = 8)
    public void Click_Salutation() {
        WebElement Salutation_DD = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/a")));
        Salutation_DD.click();
    }

    @Test(priority = 9)
    public void Salutation_Click() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/div/ul/li[5]"))
                .click();

    }

    @Test(priority = 10)
    public void employee_name() {

        WebElement empname = driver
                .findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[7]/div/input"));
        empname.sendKeys("Test2");
    }

    @Test(priority = 11)
    public void Sap_plant_code() {
        WebElement plant_code = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/a")));
        plant_code.click();

    }

    @Test(priority = 12)
    public void sap_code_set() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/div/ul/li[3]"))
                .click();

    }

    @Test(priority = 13)
    public void sap_vendor_code() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[9]/div/input"))
                .sendKeys("test_2");

    }

    @Test(priority = 14)
    public void employee_role_select() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/ul")).click();

    }

    @Test(priority = 15)
    public void select_Technician_role() {

        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/div/ul/li"))
                .click();

    }

    @Test(priority = 16)
    public void select_status() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/a")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/div/ul/li[1]"))
                .click();

    }

    @Test(priority = 17)
    public void click_save_button() {
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/button[1]")).click();

    }

    @Test(priority = 18)
    public void Record_add_notification_Check() {

        if ((new WebDriverWait(driver, 10)).until(ExpectedConditions
                .presenceOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/div/button"))) != null) {
            driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/button")).click();
        } else {
            infoBox("not Added", "Not Added");
        }

    }

    public static void infoBox(String infoMessage, String titleBar) {
        JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
    }

    public static void clickWhenReady(String location, int timeout, WebDriver driver) {

        WebElement element = null;

        WebDriverWait wait = new WebDriverWait(driver, timeout);

        element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(location)));

        element.click();

    }

    public static void clickLinkByHref(String href, WebDriver driver) {

        List<WebElement> anchors = driver.findElements(By.tagName("a"));

        Iterator<WebElement> i = anchors.iterator();

        while (i.hasNext()) {
            WebElement anchor = i.next();
            if (anchor.getAttribute("href").contains(href)) {
                anchor.click();

                break;
            }

        }

    }

}

【问题讨论】:

    标签: selenium selenium-webdriver testng qa software-quality


    【解决方案1】:

    关于你的脚本的几句话:

    1. presenceOfElementLocated(By.xpath("//*[@id=\"userId\"]")) - 如果一个元素有一个id,你应该总是尝试使用id而不是xpath

    2. By.xpath("/html/body/div/div[3]/div/div/form/div[3]/div[2]/div/button")) - 建议尽可能使用logical xpath 而不是absolute xpath。否则你的xpath 会变得脆弱。

    3. 现在您愿意遵循 POM,您需要在称为 PageFactory 的单个页面中定义页面的所有元素。同样,网页的所有元素都将驻留在单独的类中。

    例如网页上元素的条目可能如下所示:

    @FindBy(id="user_login") 
    WebElement username;
    
    1. 当您使用 TestNG 时,您可以将浏览器相关代码移动到单独的类 Browserfactory。在您的测试类中,在@BeforeTest Annotation 中调用Browserfactory 的方法来初始化浏览器,打开网址。

    这些是通过 POM 实现代码的一些基本步骤。

    如果这能解决您的问题,请告诉我。

    【讨论】:

    • 谢谢你,Dev我现在正在处理它,我非常重视你的响应。我完成后上传pom结构化代码。答案非常有帮助。谢谢!
    • @VaasPerera 很高兴能够帮助您。话虽如此,我会说,POM 还有更多内容,我会逐渐建议您使用代码可重用性和更好的性能。谢谢
    • 谢谢你,开发者!完成此操作后,我将立即上传我的代码:) 供您检查并给我反馈。对我来说,在单个文件中编写代码并运行比遵循 POM 更容易,因为我必须处理更复杂的场景,例如方法重载和覆盖,因为现在我正在重构我的代码 POM 结构,当我重用代码时,我必须思考和设计在一个文件中运行良好的方法以及它应该如何在 POM 中工作。所以有点令人沮丧对我来说......但我需要指导,请尽快上传:)
    【解决方案2】:

    这些是我根据 UI 页面创建的类,我也会将结构附加为快照 :)

    public class Driver_class {
    
        private static String baseUrl;
        private static String driverPath;
        public static WebDriver driver;
    
        public static WebDriver getDriver() {
    
            baseUrl = "http://xxx.xxx.xxx.xxx:xx:xx\\Desktop\\geckodriver.exe";
    
            if (driver == null) {
                System.setProperty("webdriver.gecko.driver", "C:\\Users\\User\\Desktop\\geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get(baseUrl);
            }
            return driver;
    
        }
    
    }
    
    
    public class Login {
    
        public WebElement uid;
        public WebElement pwd;
        public WebElement loginbtn;
    
        public void get_elements(WebDriver driver) {
            uid = (new WebDriverWait(driver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"userId\"]")));
    
            pwd = (new WebDriverWait(driver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"loginPassword\"]")));
    
            loginbtn = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                    .presenceOfElementLocated(By.xpath("/html/body/div/div[3]/div/div/form/div[3]/div[2]/div/button")));
    
        }
    
        public void customerLogin() {
    
            this.uid.sendKeys("xxxxxxxx");
            this.pwd.sendKeys("xxxxxxxxxxx");
            this.loginbtn.click();
    
        }
    
    }
    
    public class Navigation {
    
        public WebElement Personal_Tab;
        public WebElement Add_Edit_p;
    
        public void navigate_personal_button(WebDriver driver) {
    
            clickWhenReady("/html/body/div[2]/div[2]/nav/div/div[2]/div/div[1]/ul/li[5]/a", 10, driver);
    
        }
    
        public void navigate_personal_add_button(WebDriver driver) {
    
            clickLinkByHref("/rsa/5/15/staff/n/i/list", driver);
    
    
        }
    
        public static void clickWhenReady(String location, int timeout, WebDriver driver) {
    
            WebElement element = null;
    
            WebDriverWait wait = new WebDriverWait(driver, timeout);
    
            element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(location)));
    
            element.click();
    
        }
    
        public void clickLinkByHref(String href, WebDriver driver) {
    
            List<WebElement> anchors = driver.findElements(By.tagName("a"));
            Iterator<WebElement> i = anchors.iterator();
    
            while (i.hasNext()) {
                WebElement anchor = i.next();
                if (anchor.getAttribute("href").contains(href)) {
                    anchor.click();
                    break;
                }
            }
        }
    
    
    
    
    }
    public class Personal_Details {
    
        WebElement amw_radio;
        WebElement service_provider;
        WebElement Service_Provider_DD;
        //WebElement Employee_Code_Enter;
    
        WebElement Click_Salutation;
        WebElement Salutation_DD;
        WebElement empname;
        WebElement plant_code;
    
    
        //WebElement
    
        public void Radio_Button_AMW(WebDriver driver) {
             amw_radio = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                    By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[1]/div/div/label[1]")));
            amw_radio.click();
        }
    
    
        public void Radio_Button_service_provider(WebDriver driver) {
            service_provider = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                    By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[2]/div/div/label[1]")));
            service_provider.click();
        }
    
    
        public void Service_Provider_Name_select(WebDriver driver) {
    
            Service_Provider_DD = (new WebDriverWait(driver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(
                            By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[3]/div/div/a")));
            Service_Provider_DD.click();
    
            driver.findElement(By.cssSelector("ul > li:nth-child(2)")).click();
    
        }
    
    
        public void Employee_Code_Enter(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[5]/div/input"))
                    .sendKeys("01112");
    
        }
    
    
        public void Click_Salutation(WebDriver driver) {
            Salutation_DD = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                    By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/a")));
            Salutation_DD.click();
        }
    
    
        public void Salutation_Click(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[6]/div/div/div/ul/li[5]"))
                    .click();
    
        }
    
    
        public void employee_name(WebDriver driver) {
    
             empname = driver
                    .findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[7]/div/input"));
            empname.sendKeys("Test2");
        }
    
    
        public void Sap_plant_code(WebDriver driver) {
            plant_code = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
                    By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/a")));
            plant_code.click();
    
        }
    
    
        public void sap_code_set(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[8]/div/div/div/ul/li[3]"))
                    .click();
    
        }
    
    
        public void sap_vendor_code(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[9]/div/input"))
                    .sendKeys("test_2");
    
        }
    
    
        public void employee_role_select(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/ul")).click();
    
        }
    
    
        public void select_Technician_role(WebDriver driver) {
    
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[10]/div/div/div/ul/li"))
                    .click();
    
        }
    
    
        public void select_status(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/a")).click();
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[1]/div[11]/div/div/div/ul/li[1]"))
                    .click();
    
        }
    
    
        public void click_save_button(WebDriver driver) {
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/button[1]")).click();
    
        }
    
    
        public void Record_add_notification_Check(WebDriver driver) {
    
            if ((new WebDriverWait(driver, 10)).until(ExpectedConditions
                    .presenceOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/div/button"))) != null) {
                driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/button")).click();
            } else {
                infoBox("not Added", "Not Added");
            }
    
        }
    
        public static void infoBox(String infoMessage, String titleBar) {
            JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
        }
    
    
    }
    
    
    public class Personal_list {
    
        WebElement add_btn;
    
    
        public void find_add_btn(WebDriver driver) {
    
              String add_btn_path = "/html/body/div[2]/div[2]/div/div[2]/form/div[2]/div/div/a";    
              detect_element_click(driver, add_btn_path);
    
    
        }
    
        public void detect_element_click(WebDriver webDriver, String xp) {
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xp))).click();
    
        }
    }
    

    这是执行所有 test_cases 的 TestCaseClass

    public class TestCases {
    
        public WebDriver driver = Driver_class.getDriver();
    
        @Test(priority = 0)
        public void newCustomerLogin() {
    
            Login rsaLogin = PageFactory.initElements(driver, Login.class);
            rsaLogin.get_elements(driver);
            rsaLogin.customerLogin();
    
        }
    
        @Test(priority = 1)
        public void click_Personal_tab() {
    
            Navigation n = PageFactory.initElements(driver, Navigation.class);
            n.navigate_personal_button(driver);
            n.navigate_personal_add_button(driver);
    
    
        }
    
        @Test(priority=2)
        public void click_add_tab() {
    
            Personal_list ps= PageFactory.initElements(driver,Personal_list.class);
            ps.find_add_btn(driver);
    
    
        }
    
        @Test(priority=3)
        public void Enter_personal_details(){
    
            Personal_Details d= new Personal_Details();
            d.Radio_Button_AMW(driver);
            d.Radio_Button_service_provider(driver);
            d.Service_Provider_Name_select(driver);
            d.Employee_Code_Enter(driver);
            d.Click_Salutation(driver);
            d.Salutation_Click(driver);
            d.employee_name(driver);
            d.Sap_plant_code(driver);
            d.sap_code_set(driver);
            d.sap_vendor_code(driver);
            d.employee_role_select(driver);
            d.select_Technician_role(driver);
            d.select_status(driver);
            d.click_save_button(driver);
            d.Record_add_notification_Check(driver);
    
    
    
    
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多