2017-04-26 100 views
-1

我是QA自動化的新手,我在java中有一般知識, 因此我決定使用(Selenium + Java)來完成自動化。 我會將自己的代碼附加到自動化程序中,並且腳本運行順利。 但我做的結構是不正確的我想跟着(POM-硒).POM頁導向模式如何使用selenium java和(TestNG框架)設置POM結構

此腳本導航如下

登錄(頁) - > Peronal(跌落/下降選擇)---> AddEdit((Drop/down Selection)) - > Personal(Page) - > Add(Button) - > PersonalDetails(Page)

測試場景是......

用戶應該「登錄」系統,並且必須在導航標籤中點擊「個人」下拉菜單,然後會出現一個「添加編輯」下拉選擇。然後用戶被引導到頁面標題爲「個人」時,用戶應該點擊該頁面上的「添加」按鈕以引導到另一個稱爲「個人詳細信息」的頁面,用戶可以添加從頁面提供的相關字段以添加新的客戶端。

請幫我安排這個POM結構。因爲我有很難的時間去想它是怎麼回事。讓我感到沮喪的是,爲了讓用戶添加他/她應該登錄到系統的相關記錄在POM中它說登錄是一個單獨的頁面,我必須瀏覽三個頁面才能完成任務。如果你能幫助我,那麼這將是非常棒的。如果我的英語不好,可以開放討論並對不起。當涉及到自動化時,請把我當作一個總的noob。)謝謝!

這是測試腳本,我寫...

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; 
      } 

     } 

    } 

} 

回答

0

你的腳本的幾句話:

  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")) - 建議使用盡可能地使用而不是absolute xpath。否則你的xpath變得脆弱。

  3. 現在,隨着您願意遵循POM,您需要在單個頁面中定義頁面的所有元素,該頁面將稱爲PageFactory。同樣,一個網頁的所有元素都將駐留在單獨的類中。

例如,網頁上的一個元素的條目可能看起來像:

@FindBy(id="user_login") 
WebElement username; 
  • 如你用TestNG,可以移動相關的代碼到一個單獨的類Browserfactory瀏覽器。從你的測試課,在@BeforeTest註解調用方法Browserfactory來初始化瀏覽器,打開url。
  • 這些是通過POM實現您的代碼的一些基本步驟。

    讓我知道這是否解決您的查詢。

    +0

    感謝TestCaseClass你,我現在在這上面,我非常重視你的回覆。當我完成後,上傳pom結構代碼,答案非常有用。謝謝! –

    +0

    @VaasPerera我很高興我能夠幫助你。話雖如此,我會說,還有更多的POM,我會逐漸建議你爲代碼的可重複性和更好的性能。謝謝 – DebanjanB

    +0

    謝謝Dev!我會盡快上傳我的代碼:)讓我檢查並給我一個反饋。對我來說,在單個文件中編碼和運行比在POM之後運行起來很容易,因爲我必須處理更復雜的場景,例如方法重載和重寫,因爲現在我重構我的代碼POM結構,當我重新使用代碼時,我必須考慮並設計在單個文件中工作正常的方法,以及它如何在POM中工作。對我...但我希望指導,請儘快上傳:) –

    -1

    這是我創建取決於UI的網頁類,生病附加結構作爲快照太:) Structure of Eclipse

    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

    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); 
    
    
    
    
    
    
        } 
    
    } 
    
    +0

    正如我在這裏學習,如果有人這樣做,我不會有任何區別 - (減)爲我的職位。如果你願意幫忙請這樣做,謝謝! –

    +0

    本文旨在學習如何遵循POM設計框架,我認爲我可以更好地開發這個框架,並提供指導。所有專家都請求幫助! –

    相關問題