selenium实现用户中心的增删改查

package com.bw.model;
import java.util.List;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByClassName;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class UserManager {
    WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        UserManager fr = new UserManager();
        fr.initwebderiver();
        
    }
    //初始化驱动
    public void initwebderiver() throws InterruptedException  {
        boolean flag=false;
        int count=0;
        DesiredCapabilities dc = new DesiredCapabilities(); 
         dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.IGNORE); 
        System.setProperty("webdriver.firefox.bin", "C://Program Files (x86)//Mozilla Firefox//firefox.exe");
        driver = new FirefoxDriver(dc);
        //driver.get("http://123.56.92.221/mainsite/src/html/login/login.html");
        //driver.get("http://10.100.3.2/mainsite/src/html/login/login.html");    
        driver.get("http://www.baiwang.com/mainsite/src/html/login/login.html");    
    
        //driver.get("http://10.100.3.2/output/output/sysmgmt/userMgmt");        
        driver.findElement(By.id("username")).sendKeys("cpy001");
        driver.findElement(By.id("password")).sendKeys("a123456");        
        driver.findElement(By.id("login-btn")).click();
        Thread.sleep(18000);
        //tab页面切换
        List<WebElement> list=driver.findElements(By.tagName("li"));
        list.get(4).click(); 
        Thread.sleep(6000);     
        driver.manage().window().maximize(); 
        adduser();
    }
    //删除用户
    public void delete() throws InterruptedException {
         driver.findElement(By.xpath("//span[text()='查询']")).click();
            List<WebElement> delete=driver.findElements(By.xpath("//span[text()='删除']"));
            for(int i=0;i<delete.size();i++) {
                if(delete.get(i).isDisplayed()==true) {
                delete.get(i).click();                
                Thread.sleep(4000);
                break;
                }
            }
            Thread.sleep(4000);
            List<WebElement> comfire= driver.findElements(By.className("ivu-btn-large"));
            System.out.println(comfire.size());
            for(int j=0;j<comfire.size();j++) {
                if(comfire.get(j).isDisplayed()==true) {
                    comfire.get(j+1).click();
                    break;
                }
            }
    }
    //添加用户
    @Test
    public void run() throws InterruptedException {
        adduser();
        search();
    }
    
    public  void adduser() throws InterruptedException {
          driver.findElement(By.xpath("//span[text()='添加用户']")).click(); 
          Thread.sleep(7000);
         List<WebElement> listinfo=driver.findElements(By.className("ivu-input"));
           System.out.println("点击了添加按钮");
           int count=0;
           for(int i=0;i<listinfo.size();i++) {
               if(listinfo.get(i).isDisplayed()) {
                  if(i>1) {
                   String[] inputinfo= {"cpy00123","开发","shixinfa","testpart","15510225994","[email protected]"};
                       listinfo.get(i).sendKeys(inputinfo[i-13]); 
                       count++;                 
                       
                  }
                
               }
    }
           driver.findElement(By.xpath("//span[text()='下一步']")).click(); 
          
             List<WebElement> checkbox=driver.findElements(By.tagName("ivu-checkbox"));
                     //driver.findElements(By.className("ivu-checkbox-input"));
             System.out.println(checkbox.size());
             for(int i=0;i<checkbox.size();i++) {
                 if(checkbox.get(i).getAttribute("className")=="ivu-checkbox-inner") {
                     checkbox.get(i).click();
                 }
             }
             Thread.sleep(16000);
             driver.findElement(By.xpath("//div/div[2]/ul/li/label/span/input")).click();
             Thread.sleep(14000);
             driver.findElement(By.xpath("//form[2]/div/div/div/div/div/div[2]/ul/li/div/div/ul[4]/li/label/span/input")).click();
             Thread.sleep(5000);
             driver.findElement(By.xpath("//div[2]/div/div/div[3]/div/button[3]/span")).click();
             Thread.sleep(5000);
             driver.findElement(By.xpath("//div/div/div[3]/div/button[5]")).click(); 
          
          //li/div/div/ul[3]/li/label/span/input
           
          // driver.findElement(By.cssSelector("body > div:nth-child(12) > div.ivu-modal-wrap.addUserBox > div > div > div.ivu-modal-body > div.usermgmt_mdel > form:nth-child(2) > div > div > div > div > div > div.tree_node_box > ul > li > label > span > input")).click();
         
           
           /*for(int i=0;i<checkbox.size();i++) {
//              / &&checkbox.get(i).getAttribute("className")=="ivu-checkbox-input"
               if(checkbox.get(i).getAttribute("type")=="checkbox") {
                   checkbox.get(i).click();
               }
             // System.out.println(checkbox.get(i).getAttribute("className"));
              System.out.println(checkbox.get(i).getAttribute("type"));
           }*/
           
}
    //查詢數據
    public void search() throws InterruptedException {
          List<WebElement> search=driver.findElements(By.className("ivu-input"));
          System.out.println(search.size());
           /* for(int k=0;k<search.size();k++) { 
                if(search.get(k).getAttribute("type")=="text"&&search.get(k).getAttribute("spellcheck")=="false") {
                    System.out.println("进入到It行业");
                    
                }*/
                //System.out.println(search.get(k).getAttribute("placeholder")+search.get(k).getAttribute("autocomplete"));
           search.get(0).sendKeys("cpy001");
             search.get(1).sendKeys("测试40");
            driver.findElement(By.className("bw-chaxun")).click(); 
            Thread.sleep(2000);            
    }
    //編輯數據
    public void edituser() throws InterruptedException {
        driver.findElement(By.xpath("//span[text()='添加用户']")).click(); 
        Thread.sleep(7000);
         List<WebElement> listinfo=driver.findElements(By.className("ivu-input"));
           System.out.println("点击了添加按钮");
           int count=0;
           for(int i=0;i<listinfo.size();i++) {
               if(listinfo.get(i).isDisplayed()) {
                  if(i>1) {
                   String[] inputinfo= {"cpy00123","开发","shixinfa","testpart","15510225994","[email protected]"};
                       listinfo.get(i).sendKeys(inputinfo[i-13]); 
                       count++;                 
                       
                  }
                
               }
    }
           driver.findElement(By.xpath("//span[text()='下一步']")).click(); 
          
             List<WebElement> checkbox=driver.findElements(By.tagName("ivu-checkbox"));
                     //driver.findElements(By.className("ivu-checkbox-input"));
             System.out.println(checkbox.size());
             for(int i=0;i<checkbox.size();i++) {
                 if(checkbox.get(i).getAttribute("className")=="ivu-checkbox-inner") {
                     checkbox.get(i).click();
                 }
             }
             Thread.sleep(12000);
           driver.findElement(By.xpath("//div/div[2]/ul/li/label/span/input")).click();
           Thread.sleep(14000);
           driver.findElement(By.xpath("//form[2]/div/div/div/div/div/div[2]/ul/li/div/div/ul[4]/li/label/span/input")).click();
           Thread.sleep(5000);
           driver.findElement(By.xpath("//div[2]/div/div/div[3]/div/button[3]/span")).click();
           Thread.sleep(5000);
           driver.findElement(By.xpath("//div/div/div[3]/div/button[5]")).click(); 
        
        //li/div/div/ul[3]/li/label/span/input
           
          // driver.findElement(By.cssSelector("body > div:nth-child(12) > div.ivu-modal-wrap.addUserBox > div > div > div.ivu-modal-body > div.usermgmt_mdel > form:nth-child(2) > div > div > div > div > div > div.tree_node_box > ul > li > label > span > input")).click();
         
           
           /*for(int i=0;i<checkbox.size();i++) {
//              / &&checkbox.get(i).getAttribute("className")=="ivu-checkbox-input"
               if(checkbox.get(i).getAttribute("type")=="checkbox") {
                   checkbox.get(i).click();
               }
             // System.out.println(checkbox.get(i).getAttribute("className"));
              System.out.println(checkbox.get(i).getAttribute("type"));
           }*/
    
}
      
}

       
---------------------
作者:言寡
来源:CSDN
原文:https://blog.csdn.net/qq_21406125/article/details/86141449
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/weixin_37565521/article/details/86141622