Java&SeleniumのPOモード【ページ要素、ページオブジェクトおよびテストコード分離】

15549 ワード

Java&SeleniumのPOモード【ページ要素、ページオブジェクトおよびテストコード分離】
一、要約
このブログでは、自動化テストの実装ページ要素、ページオブジェクト、およびテストコードの自動化フレームワークからの分離の実装について説明します.
二、ページ要素の位置決め情報を解析する
まず、ページ要素と実際のコードを分離し、まずページ要素の位置決め情報と位置決め式を属性ファイルに保存します.例えば、PaaSプラットフォームがMysqlサービスを提供するページなど、プロジェクトでMysqlServicesという名前のページを新規作成します.propertiesのファイルです.ファイルに保存されている内容は次のようです.
[MySQL     ]
[MySQL     -  ]
paas.mysql.refreshbutton=xpath>//*[@id='app']/section/section/main/div[2]/div/div[5]/button
paas.mysql.createnewinstance=xpath>//*[@id='app']/section/section/main/div[2]/div/div[4]/button
paas.mysql.searchinstancenameinput=xpath>//*[@id="app"]/section/section/main/div[2]/div/div[1]/label[1]/div/input
paas.mysql.searchinstancenamebutton=xpath>//*[@id='app']/section/section/main/div[2]/div/div[1]/label[2]/button
paas.mysql.searchspacename=xpath>//*[@id='app']/section/section/main/div[2]/div/div[3]/label[2]/div/div[1]/input
paas.mysql.operation=xpath>//*[@id='app']/section/section/main/section/div[1]/div[3]/table/tbody/tr[1]/td[6]/div/div/span
paas.mysql.operationrestart=xpath>/html/body/ul/li[1]
paas.mysql.operationrelease=xpath>/html/body/ul/li[2]
paas.mysql.operationmanage=xpath>/html/body/ul/li[3]
paas.mysql.operationlog=xpath>/html/body/ul/li[4]
paas.mysql.operationmonitor=xpath>/html/body/ul/li[5]
paas.mysql.confirmrestart=xpath>/html/body/div[1]/div/div[3]/button[2]
paas.mysql.cancelrestart=xpath>/html/body/div[1]/div/div[3]/button[1]
paas.mysql.releaseconfirmbutton=xpath>/html/body/div[1]/div/div[3]/button[2]
paas.mysql.releasecancelbutton=xpath>/html/body/div[1]/div/div[3]/button[1]

[MySQL     -  ]
paas.newinstance.instancename=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[2]/div/div[1]/input
paas.newinstance.description=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[10]/div/div/textarea
paas.newinstance.standard5.6=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[4]/div/div/label[1]/span
paas.newinstance.standard5.7=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[4]/div/div/label[2]/span
paas.newinstance.instancestandard=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[5]/div/div/div[1]/input
paas.newinstance.1c1gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[1]
paas.newinstance.1c2gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[2]
paas.newinstance.2c8gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[3]

三、解析位置決め要素属性ファイル
seleniumを満たす8中位置決め方式
/*
 * @FileName GetElementUtil: this util is use for getting page element
 * @author davieyang
 * @create 2018-08-21 16:37
 */
package util;
import org.openqa.selenium.By;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class GetElementUtil {
    private Properties properties;
    /**
     *                
     * @param propFile          ,      
     */
    public GetElementUtil(String propFile){
        properties = new Properties();
        try{
            FileInputStream in = new FileInputStream(propFile);
            properties.load(in);
            in.close();
        }catch (IOException e){
            System.out.println("        ");
            e.printStackTrace();
        }
    }

    /**
     * @param elementName              "pass.spacemanagement.releasebutton"
     * @return               ,      
     * @throws Exception "   locator Type        :" + locatorType
     */
    public By getLocator(String elementName) throws Exception{
        //    ElementNameInproFile,                 
        String locator = properties.getProperty(elementName);
        //             locatorType  ,          locatorValue  
        String locatorType = locator.split(">")[0];
        String locatorValue = locator.split(">")[1];
        /**
         *         ISO-8859-1    ,  getBytes             UTF-8
         *                
         */
        locatorValue = new String(locatorValue.getBytes("ISO-8859-1"), "UTF-8");
        //  locatorType    locatorValue   ,        
        System.out.println("       :" + locatorType + "\t        " + locatorValue);
        //  locatorType                 By  
        if(locatorType.toLowerCase().equals("id"))
            return By.id(locatorValue);
        else if(locatorType.toLowerCase().equals("name"))
            return By.name(locatorValue);
        else if(locatorType.toLowerCase().equals("classname")||(locatorType.toLowerCase().equals("class")))
            return By.className(locatorValue);
        else if(locatorType.toLowerCase().equals("tagname")||(locatorType.toLowerCase().equals("tag")))
            return By.tagName(locatorValue);
        else if(locatorType.toLowerCase().equals("linktext")||(locatorType.toLowerCase().equals("link")))
            return By.linkText(locatorValue);
        else if(locatorType.toLowerCase().equals("partiallinktext"))
            return By.partialLinkText(locatorValue);
        else if(locatorType.toLowerCase().equals("cssselector")||(locatorType.toLowerCase().equals("css")))
            return By.cssSelector(locatorValue);
        else if(locatorType.toLowerCase().equals("xpath"))
            return By.xpath(locatorValue);
        else
            throw new Exception("   locator Type        :" + locatorType);
    }
}

四、ページ要素をオブジェクトにカプセル化する
/*
 * @FileName StoreManagement:   Mysql    
 * @outhor davieyang
 * @create 2018-08-08 11:12
 */
package pageobject.resourcemanagement;
import util.GetElementUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static constants.Constants.MysqlService_Property;
import static util.JavaScriptToDo.highLightElement;

public class MySQLService {
    private static WebElement element = null;
    /**                    
     *
     */
    private static GetElementUtil getElementUtil = new GetElementUtil(MysqlService_Property);
    private WebDriver driver;
    public MySQLService(WebDriver driver){
        this.driver = driver;
    }

    /**  MySQL       “  ”         
     *
     * @param driver      
     * @return
     * @throws Exception         
     */
    public static WebElement refresh_Button(WebDriver driver) throws Exception{
        //  GetElementUtil  getLocator                         
        element = driver.findElement(getElementUtil.getLocator("paas.mysql.refreshbutton"));
        highLightElement(driver, element);
        return element;
    }

    /**  MySQL       “    ”         
     *
     * @param driver      
     * @return
     * @throws Exception         
     */
    public static WebElement create_New_Instance_Button(WebDriver driver) throws Exception{
        //  GetElementUtil  getLocator                         
        element = driver.findElement(getElementUtil.getLocator("paas.mysql.createnewinstance"));
        highLightElement(driver, element);
        return element;
    }

    /**
     *
     * @param driver
     * @return
     * @throws Exception
     */
    public static WebElement search_Instance_Name_Input(WebDriver driver) throws Exception{
        //  GetElementUtil  getLocator                         
        element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchinstancenameinput"));
        highLightElement(driver, element);
        return element;
    }
    /**  MySQL       MySQL             
     *
     * @param driver      
     * @return
     * @throws Exception         
     */
    public static WebElement search_Instance_Name_Button(WebDriver driver) throws Exception{
        //  GetElementUtil  getLocator                         
        element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchinstancenamebutton"));
        highLightElement(driver, element);
        return element;
    }

    /**  MySQL                     
     *
     * @param driver      
     * @return
     * @throws Exception         
     */
    public static WebElement search_Space_Name(WebDriver driver) throws Exception{
        //  GetElementUtil  getLocator                         
        element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchspacename"));
        highLightElement(driver, element);
        return element;
    }
}

五、テストコード
package testscript;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
import util.KeyActionsUtil;
import static util.KeyActionsUtil.*;
import java.util.List;
import static appmodule.MysqlService.linkToMysqlPage;
import static util.KeyBoardUtil.pressTabKey;
import static util.LogUtil.info;
import static pageobject.resourcemanagement.MySQLService.*;
import static util.ScrollBarUtil.scrolltoBottom;
import static util.WaitElementUtil.sleep;
// @Listeners({util.TestReport.class})
public class Test_Mysql {

    static {
        DOMConfigurator.configure("log4j.xml");
    }
    @BeforeClass
    public void setUp()throws Exception {
        WebDriver driver = KeyActionsUtil.initBrowser("chrome");
        linkToMysqlPage(driver, "yangdawei", "alex005x");
        sleep(2000);
    }

    @Test(priority = 0, description = "    mysql     1CPU2G")
    public void test_CreateMysqlInstance() throws Exception {
        create_New_Instance_Button(driver).click();
        info("        ...");
        sleep(1000);
        info("  3 ...");
        instance_Name_in_Create_Instance_Dialog(driver).sendKeys("automationtest");
        info("     :automationtesta");
        sleep(1000);
        info("  3 ...");
        //           ,     list ,     
        List radios = driver.findElements(By.className("el-radio-button__inner"));
        radios.get(1).click();
        sleep(1000);
        info("       5.7...");
        instance_Standard_in_Create_Instance_Dialog(driver).click();
        info("      ...");
        sleep(2000);
        info("  2 ...");
        one_Core_two_GB(driver).click();
        info("  1CPU2GB...");
        storage_Space_in_Create_Instance_Dialog(driver).clear();
        info("        ...");
        storage_Space_in_Create_Instance_Dialog(driver).sendKeys("1");
        info("  1G....");
        scrolltoBottom(driver);
        sleep(2000);
        pressTabKey();
        outsideaccess_Checkbox_in_Create_Instance_Dialog(driver).click();
        info("      ...");
        password_in_Create_Instance_Dialog(driver).sendKeys("111111");
        info("    111111...");
        repassword_in_Create_Instance_Dialog(driver).sendKeys("111111");
        info("    111111...");
        description_in_Create_Instance_Dialog(driver).sendKeys("automationtest");
        info("      automationtest");
        sleep(2000);
        submit_Button_in_Create_Instance_Dialog(driver).sendKeys(Keys.ENTER);
        info("    ...");
        sleep(2000);
        refresh_Button(driver).click();
        Assert.assertTrue(driver.getPageSource().contains("automationtest"));
        Assert.assertTrue(driver.getPageSource().contains("   "));
    }
    @Test(priority = 1, description = "  mysql  ")
    public void test_RestartMysqlInstance()throws Exception {
        operation_Button(driver).click();
        info("          ...");
        sleep(2000);
        info("  3 ...");
        operation_Restart_Button(driver).click();
        info("            ...");
        sleep(2000);
        info("  3 ...");
        restart_Confirm_Button(driver).click();
        info("      ...");
        sleep(2000);
        info("  3 ...");
        Assert.assertTrue(driver.getPageSource().contains("      "));
        Assert.assertTrue(driver.getPageSource().contains("   "));
    }

    @Test(priority = 2, description = "  mysql    ")
    public void test_Review_Basic_Mysql_Info()throws Exception{
        operation_Button(driver).click();
        info("          ...");
        sleep(2000);
        info("  3 ...");
        operation_Manage_Button(driver).click();
        info("            ...");
        sleep(2000);
        info("    ");
        assertString(driver,"    ");
    }
    @Test(priority = 3, description = "  mysql    ")
    public void test_Review_Mysql_Link()throws Exception{
        database_Link_Tab(driver).click();
        sleep(2000);
        Assert.assertTrue(driver.getPageSource().contains("210.13.50.105"));
    }

    @Test(priority = 4,description = "  Mysql  ")
    public void test_ReviewLog()throws Exception{
        operation_Button(driver).click();
        info("          ...");
        sleep(2000);
        info("  3 ...");
        operation_Log_Button(driver).click();
        info("            ...");
        sleep(2000);
        info("  3 ...");
        extend_Button_in_Log_Page(driver).click();
        info("      ...");
        sleep(2000);
        info("  3 ...");
        datefrom_in_Log_Page(driver).click();
        info("         ,    ...");
        sleep(2000);
        info("  3 ...");
        datefrom_by_Date_in_Log_Page(driver).clear();
        datefrom_by_Date_in_Log_Page(driver).sendKeys("2018-09-01");
        info("    ”2018-09-01");
        sleep(2000);
        info("  3 ...");
        datefrom_Sure_Button_in_Log_Page(driver).click();
        info("      ...");
        sleep(2000);
        info("  3 ...");
        search_Button_in_Log_Page(driver).click();
        info("      ...");
        sleep(2000);
        info("  3 ...");
        Assert.assertTrue(driver.getPageSource().contains("Initializing database"));

    }

    @Test(priority = 5, description = "  Mysql    ")
    public void test_MonitorMysqlService()throws Exception{
        operation_Button(driver).click();
        info("          ...");
        sleep(3000);
        info("  3 ...");
        operation_Monitor_Button(driver).click();
        info("            ...");
        sleep(3000);
        info("  3 ...");
    }

    @Test(priority = 6, description = "  mysql  ")
    public void test_ReleaseMysqlService()throws Exception{
        operation_Button(driver).click();
        info("          ...");
        sleep(3000);
        info("  3 ...");
        operation_Release_Button(driver).click();
        info("            ...");
        sleep(3000);
        info("  3 ...");
        release_Confirm_Button(driver).click();
        info("      ...");
        sleep(3000);
        info("  3 ...");
        Assert.assertTrue(driver.getPageSource().contains("    "));
        Assert.assertTrue(driver.getPageSource().contains("   "));
    }

    @AfterClass
    public void afterMethod(){
        driver.quit();
    }
}