wordpress 登录实例(一)
wordpress搭建参考
关于wordpress环境的搭建,地址是:http://easonhan007.github.io/2014/04/08/install-wamp-and-wordpress/
按照步骤一步一步来就可以搞定。非常值得参考!
先说下,实例操作之前的一些东西,新建一个java项目,然后导入selenium的jar包
这里是2个selenium的jar包
如图
代码如下
1 package info.itest.www; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.SearchContext; 10 import org.openqa.selenium.WebDriver; 11 import org.openqa.selenium.WebElement; 12 import org.openqa.selenium.chrome.ChromeDriver; 13 import org.openqa.selenium.firefox.FirefoxDriver; 14 15 public class RunAll { 16 WebDriver dr; 17 18 @Before 19 public void setUp() throws Exception { 20 21 // 使用firefox浏览器,打开指定路径的firefox浏览器 22 System.setProperty("webdriver.firefox.bin", 23 "D:/Mozilla Firefox/firefox.exe"); 24 this.dr = new FirefoxDriver(); 25 26 //使用chrome浏览器 27 //this.dr = new ChromeDriver(); 28 29 } 30 31 @After 32 public void tearDown() throws Exception { 33 34 this.dr.quit(); 35 36 } 37 38 @Test 39 public void testLogin() { 40 41 String userName = "admin"; 42 String password = "123456"; 43 //定义登录url 44 this.dr.get("http://localhost/wordpress/wp-login.php"); 45 //输入用户名和密码,点击登录 46 dr.findElement(By.id("user_login")).sendKeys(userName); 47 dr.findElement(By.id("user_pass")).sendKeys(password); 48 dr.findElement(By.id("wp-submit")).click(); 49 50 //断言登录用户 51 assertTrue(dr.getCurrentUrl().contains(userName)); 52 53 //断言用户是否登录成功 54 WebElement adminLink = dr.findElement(By.id("wp-admin-bar-my-account")).findElement(By.className("ab-item")); 55 assertTrue(adminLink.getText().contains(userName)); 56 57 58 59 } 60 }
运行结果截图
//login重构如下
package info.itest.www; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.SearchContext; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class RunAll { WebDriver dr; @Before public void setUp() throws Exception { // 使用firefox浏览器,打开指定路径的firefox浏览器 System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); this.dr = new FirefoxDriver(); //使用chrome浏览器 //this.dr = new ChromeDriver(); } @After public void tearDown() throws Exception { this.dr.quit(); } @Test public void testLogin() { String username = "admin"; String password = "123456"; //定义登录url this.Login(username, password); //断言登录用户 assertTrue(dr.getCurrentUrl().contains(username)); //断言用户是否登录成功 WebElement adminLink = dr.findElement(By.id("wp-admin-bar-my-account")).findElement(By.className("ab-item")); assertTrue(adminLink.getText().contains(username)); } @Test public void testCreatPost(){ fail("Not yet implemented"); } @Test public void testDeletePost(){ fail("Not yet implemented"); } @Test public void TestView(){ } @Test public void TestRevisePost(){ } public void Login(String username,String password){ this.dr.get("http://localhost/wordpress/wp-login.php"); //输入用户名和密码,点击登录 dr.findElement(By.id("user_login")).sendKeys(username); dr.findElement(By.id("user_pass")).sendKeys(password); dr.findElement(By.id("wp-submit")).click(); } }