1, modified Login class added assertions;
Assertion: Check our operating results pages get is consistent with the results we expected.
2, using the xml file to run all the test classes;
Login class two test write:
package com.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class Login { Driver the WebDriver = null ; // call the method of positioning elements ElementLocation elementLocation = new new ElementLocation (); // run before running a method @BeforeMethod public void before () { System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver.exe"); driver = new ChromeDriver(); String url = "http://xadev.alsolife.com/"; driver.manage().window().maximize(); driver.get(url); } /** * Positioning login interface elements * 1. Enter the correct phone number * 2. Enter the correct password * 3. Log success */ // @Test // public void test_login1(){ // elementLocation.findElementByCssClearSendkeys("input[type='text']","15211111111",driver); // elementLocation.findElementByCssClearSendkeys("input[type='password']","123456",driver); // elementLocation.findElementByCssClick("button[type='button']",driver); // System.out.println("登录成功,跳转到首页"); // } // enter the wrong user name @Test public void test_login2 () { String phone = "153"; elementLocation.findElementByCssClearSendkeys("input[type='text']",phone,driver); elementLocation.findElementByCssClick("button[type='button']",driver);
//加入断言 try{ Assert.assertEquals(phone,"15211111111"); }catch(AssertionError e){ System.out.println ( "incorrect phone number format:" + e.getMessage ()); } } // do not enter the phone number @Test public void test_login3 () { Phone String = "" ; // enter the phone number elementLocation.findElementByCssClearSendkeys ( "the INPUT [of the type = 'text']", Phone , Driver); // Click Login elementLocation.findElementByCssClick ( "the Button [of the type = 'the Button']" , Driver); the try { Assert.assertEquals(phone,"15211111111"); }catch (AssertionError e){ System.out.println ( "phone number can not be empty" + e.getMessage ()); } } // run after completion of a method of operating @AfterMethod public void After () { the try { Thread thread = new Thread(); thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace (); } driver.quit(); } }
TestSuit.xml create a file (name easily play):
<suite name="TestSuite Demo"> <test name="TestSuite Demo Test"> <classes> <! - execute test case class ->
<- name:! Encased executive class
<class> have more -> <class name="com.test.Login"></class> </classes> </test> </suite>
Run directly TestSuit.xml a file, the Login class.
Results are as follows:
content:
1, TestNG assert methods commonly used:
assertEquals (String actual, String expected) // determine the true value of the expected values are equal, if not equal the test fails will throw an exception
assertEqual (Actual String, String expected, Stringmessage) // check if two strings are equal, if not equal, the test failed, and throw in the third message parameter information we provide exceptions to print out
assertTrue (boolean condition) // if the value is true, by the use cases, or throw an exception AssertionError
assertFalse(boolean condition)
2, the order of execution of the test cases, Login class two test cases: test_login2, test_login3
Generally based on character ordering, if the same character in digital sort.
Talk about the previous question:
1, before the date, success has not been saved, because the button element positioned the wrong way:
Before writing: driver.findElement (By.ByXPath.xpath ( " (// Button [@ type = 'Button']) [. 1]") ) .click ();
改正之后: driver.findElement(By.ByXPath.xpath("//button[contains(@class,'submit-infor')]")).click();
Always I thought it was the wrong date positioned elements have been modified, and finally found the button is positioned incorrectly, but there are doubts: other content can not successfully saved to date.