西部数码主机 | 阿里云主机| 虚拟主机 | 服务器 | 返回乐道官网
当前位置: 主页 > php教程 > wordpress教程 >

wordpress登录、修改、删除、查看代码记录

时间:2016-11-30 09:31来源:未知 作者:好模板 点击:
wordpress登录,新增、修改、删除、查看,页面代码如下 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.o

wordpress登录,新增、修改、删除、查看,页面代码如下

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.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
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;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class RunAll {
    WebDriver dr;

    @Before
    public void setUp() throws Exception {
        System.out.println("Start test");
        
        // 使用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 {
        System.out.println("End test");
        this.dr.quit();
        
    }

    @Test
    public void testLogin() {
        System.out.println("login test");
        
        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 testCreatePost(){
        System.out.println("create test");
        
        String username = "admin";
        String passwd = "123456";
        //登录
        this.Login(username, passwd);
        
        String title = "Test title" + System.currentTimeMillis();
        this.createPost(title);
        
        dr.get("http://localhost/wordpress/");
        WebElement first_post = dr.findElement(By.cssSelector(".entry-title a"));
        System.out.println(first_post);
        assertEquals(title,first_post.getText());        
                
                
    }
    
    @Test(expected=NoSuchElementException.class)
    public void testDeletePost(){
        System.out.println("delete test");
        
        String username = "admin";
        String passwd = "123456";
        //登录
        this.Login(username, passwd);
        
        //创建文章
        String postId = this.createPost();
        
        this.dr.get("http://localhost/wordpress/wp-admin/edit.php");
        
        
        
        //计算文章id
        String rowId = "post-"+postId;
        WebElement row = this.dr.findElement(By.id(rowId));
        
        
        //执行删除
        Actions action = new Actions(this.dr);
        action.moveToElement(row).perform();
        row.findElement(By.cssSelector(".trash a")).click();
        
        //assertFalse(this.isPostRowExists(rowId));
        
        
        this.dr.findElement(By.id(rowId));
        
    }
    
    @Test
    public void TestView(){
        System.out.println("view test");
        String username = "admin";
        String password = "123456";
        this.Login(username, password);
        
        //创建文章
        String title = "Test title" + System.currentTimeMillis();
        String postId = this.createPost(title);
        //String postId = this.createPost();

        this.dr.get("http://localhost/wordpress/wp-admin/edit.php");

        // 计算文章id
        String rowId = "post-" + postId;
        WebElement row = this.dr.findElement(By.id(rowId));

        // 执行查询
        Actions action = new Actions(this.dr);
        action.moveToElement(row).perform();
        row.findElement(By.cssSelector(".view a")).click();
        
        
        //断言
        
        //dr.get("http://localhost/wordpress/?p="+postId);
        
        System.out.println(dr.findElement(By.cssSelector(".entry-title")));
        WebElement first_post = dr.findElement(By.cssSelector(".entry-title"));
        
        assertEquals(title,first_post.getText());    
        
        
    }
    
    @Test
    public void TestRevisePost(){
        System.out.println("revise test");
        
        
        String username = "admin";
        String passwd = "123456";
        //登录
        this.Login(username, passwd);
        //创建
        String postId = this.createPost();
        
        dr.get("http://localhost/wordpress/wp-admin/edit.php");
        //获取id
        String rowId = "post-"+postId;
        WebElement row = this.dr.findElement(By.id(rowId));
        //执行修改
        Actions action = new Actions(this.dr);
        action.moveToElement(row).perform();
        row.findElement(By.cssSelector(".edit a")).click();
        
        //dr.get("http://localhost/wordpress/wp-admin/post.php?post="+postId+"&action=edit");
        

        dr.findElement(By.name("post_title")).clear();
        this.setContent(null);
        String title = "Test title" + System.currentTimeMillis();
        String content = "Test title" + System.currentTimeMillis();
        dr.findElement(By.name("post_title")).sendKeys(title);
        this.setContent(content);
        dr.findElement(By.id("publish")).click();
        //this.createPost(title);
        
        dr.get("http://localhost/wordpress/");
        WebElement first_post = dr.findElement(By.cssSelector(".entry-title a"));
        System.out.println(first_post);
        assertEquals(title,first_post.getText());
        
    }
    
    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();
    }
    
    public void setContent(String content) {

        String js = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML='"
                + content + "'";
        ((JavascriptExecutor) dr).executeScript(js);
    }
    
    public String createPost(String title,String content){
        
        this.dr.get("http://localhost/wordpress/wp-admin/post-new.php");
        //String title = "Test title" + System.currentTimeMillis();
        //this.dr.findElement(By.name("post_title")).clear();
        this.dr.findElement(By.name("post_title")).sendKeys(title);
        this.setContent(content);
        dr.findElement(By.name("publish")).click();
        return this.fetchPostId();
    }
    
    public String fetchPostId(){
        String postUrl = this.dr.findElement(By.id("sample-permalink")).getText();
        String[] arr = postUrl.split("=");
        return arr[1];
    }
    
    public String createPost(){
        String title = "Test title" + System.currentTimeMillis();
        String content = "Test Content" + System.currentTimeMillis();
        return this.createPost(title,content);
    }
    
    public String createPost(String title){
        String content = "test Content" + System.currentTimeMillis();
        return this.createPost(title,content);
    }
    
    /*public boolean isPostRowExists(String rowId){
        
        try{
            this.dr.findElement(By.cssSelector(rowId));
        return true;
        }catch(NoSuchElementException e){
            return false;
        }
    }*/
    
        
    }

    

查看文章思路

 

 

 

 修改文章代码有点重复,暂时未重构

(责任编辑:好模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容