selenium-网站demo学习-test Design-优化自动化代码

Posted zhizhiyin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium-网站demo学习-test Design-优化自动化代码相关的知识,希望对你有一定的参考价值。

看selenium的网站的文档,里面的自动化用例设计有一些小点很靠谱。学了很多,可以用作优化自己的代码。

1.测试类型:

Testing Static Content

Testing Links

Function Tests

Testing Dynamic Elements

Ajax Tests

 

Assert vs. Verify

  assert 和verify的区别:assert如果wrong,就会停止执行后面的内容;verify如果wrong,会记录下来,然后继续执行后面的内容。

Choosing a Location Strategy

  1.id和name是最高效速度最快的;

  2.xpath是万能的

Wrapping Selenium Calls

包装一下selenium的方法,减少代码冗余

---click方法

  原来的代码:  

selenium.click(elementLocator);
selenium.waitForPageToLoad(waitPeriod);

  优化后的代码:

/**
 * Clicks and Waits for page to load.
 *
 * param elementLocator
 * param waitPeriod
 */
public void clickAndWait(String elementLocator, String waitPeriod) {
        selenium.click(elementLocator);
        selenium.waitForPageToLoad(waitPeriod);
}

----操作元素,这个其实有用过,但是没有全面包装:

/**
 * Selenium-WebDriver -- Clicks on an element only if it is available on a page.
 *
 * param elementLocator
 */
public void safeClick(String elementLocator) {
        WebElement webElement = getDriver().findElement(By.XXXX(elementLocator));
        if(webElement != null) {
                selenium.click(webElement);
        } else {
                // Using the TestNG API for logging
                Reporter.log("Element: " + elementLocator + ", is not available on a page - "
                                + getDriver().getUrl());
        }
}

  

 

‘Safe Operations’ for Element Presence

/**
 * Selenium-WebDriver -- Clicks on an element only if it is available on a page.
 *
 * param elementLocator
 */
public void safeClick(String elementLocator) {
        WebElement webElement = getDriver().findElement(By.XXXX(elementLocator));
        if(webElement != null) {
                selenium.click(webElement);
        } else {
                // Using the TestNG API for logging
                Reporter.log("Element: " + elementLocator + ", is not available on a page - "
                                + getDriver().getUrl());
        }
}

  

User Interface Mapping

To summarize, a UI Map has two significant advantages

  • Using a centralized location for UI objects instead of having them scattered throughout the script. This makes script maintenance more efficient.
  • Cryptic html Identifiers and names can be given more human-readable names improving the readability of test scripts.
  • public void testNew() throws Exception {
                 selenium.open("http://www.test.com");
                 selenium.type("loginForm:tbUsername", "xxxxxxxx");
                 selenium.click("loginForm:btnLogin");
                 selenium.click("adminHomeForm:_activitynew");
                 selenium.waitForPageToLoad("30000");
                 selenium.click("addEditEventForm:_IDcancel");
                 selenium.waitForPageToLoad("30000");
                 selenium.click("adminHomeForm:_activityold");
                 selenium.waitForPageToLoad("30000");
    }
    

      优化为

  • public void testNew() throws Exception {
                 selenium.open("http://www.test.com");
                 selenium.type(admin.username, "xxxxxxxx");
                 selenium.click(admin.loginbutton);
                 selenium.click(admin.events.createnewevent);
                 selenium.waitForPageToLoad("30000");
                 selenium.click(admin.events.cancel);
                 selenium.waitForPageToLoad("30000");
                 selenium.click(admin.events.viewoldevents);
                 selenium.waitForPageToLoad("30000");
    }
    

      和properties

  • admin.username = loginForm:tbUsername
    admin.loginbutton = loginForm:btnLogin
    admin.events.createnewevent = adminHomeForm:_activitynew
    admin.events.cancel = addEditEventForm:_IDcancel
    admin.events.viewoldevents = adminHomeForm:_activityold
    

      ↑ 这种方法,一般在java中的实现,我看到的,都是用class,不是用prop.properties

  •  

Page Object Design Pattern

The Page Object Design Pattern provides the following advantages

1. 分离测试代码和页面代码,比如定位和布局;

2. 把页面的服务或者操作存在一个简单的仓库里面,而不是散乱的在测试用例中;

 

We encourage the reader who wishes to know more to search the internet for blogs on this subject. 

之前其实在一些讨论里面看到过page object设计模式,不过具体实现的话,以前曾经做过登录页面的一点点,没有全部页面应用。感觉可以优化一下现有代码。

优化前的实现:

/***
 * Tests login feature
 */
public class Login {

        public void testLogin() {
                selenium.type("inputBox", "testUser");
                selenium.type("password", "my supersecret password");
                selenium.click("sign-in");
                selenium.waitForPageToLoad("PageWaitPeriod");
                Assert.assertTrue(selenium.isElementPresent("compose button"),
                                "Login was unsuccessful");
        }
}

  

There are two problems with this approach.

  1. There is no separation between the test method and the AUT’s locators (IDs in this example); both are intertwined in a single method. If the AUT’s UI changes its identifiers, layout, or how a login is input and processed, the test itself must change.
  2. The ID-locators would be spread in multiple tests, in all tests that had to use this login page.

e.g

优化后的代码:将page变成对象,操作是属于该page的

/**
 * Page Object encapsulates the Sign-in page.
 */
public class SignInPage {

        private Selenium selenium;

        public SignInPage(Selenium selenium) {
                this.selenium = selenium;
                if(!selenium.getTitle().equals("Sign in page")) {
                        throw new IllegalStateException("This is not sign in page, current page is: "
                                        +selenium.getLocation());
                }
        }

        /**
         * Login as valid user
         *
         * @param userName
         * @param password
         * @return HomePage object
         */
        public HomePage loginValidUser(String userName, String password) {
                selenium.type("usernamefield", userName);
                selenium.type("passwordfield", password);
                selenium.click("sign-in");
                selenium.waitForPageToLoad("waitPeriod");

                return new HomePage(selenium);
        }
}

  

/**
 * Page Object encapsulates the Home Page
 */
public class HomePage {

        private Selenium selenium;

        public HomePage(Selenium selenium) {
                if (!selenium.getTitle().equals("Home Page of logged in user")) {
                        throw new IllegalStateException("This is not Home Page of logged in user, current page" +
                                        "is: " +selenium.getLocation());
                }
        }

        public HomePage manageProfile() {
                // Page encapsulation to manage profile functionality
                return new HomePage(selenium);
        }

        /*More methods offering the services represented by Home Page
        of Logged User. These methods in turn might return more Page Objects
        for example click on Compose mail button could return ComposeMail class object*/

}

  用例:

/***
 * Tests login feature
 */
public class TestLogin {

        public void testLogin() {
                SignInPage signInPage = new SignInPage(selenium);
                HomePage homePage = signInPage.loginValidUser("userName", "password");
                Assert.assertTrue(selenium.isElementPresent("compose button"),
                                "Login was unsuccessful");
        }
}

  

上面的代码应该是seleniumRC?不是最新的selenium3.0的实现。

 

a few basic rules:

  1.Page objects themselves should never make verifications or assertions.

  页面对象不做校验和断言。校验和断言属于测试用例,必须在测试代码中实现,而不是在页面对象中。页面对象只包含页面表达, 和服务于页面的方法,和要测试的代码无关。

  2.There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly. 

  有唯一的能被正确定位的页面元素,在页面对象中,可以用来验证这个页面。

这里介绍的方法其实不是很懂,需要多看下其他的介绍。

 

以上是关于selenium-网站demo学习-test Design-优化自动化代码的主要内容,如果未能解决你的问题,请参考以下文章

Selenium3.14.1+Python安装和第一个Demo

selenium demo

selenium - unittest一个简单的demo

selenium - SMTP发送邮件 - 完整示例demo

Selenium+Python进行web自动化测试(Demo+API)

Selenium 2自动化测试实战34(编写Web测试用例)