zoukankan      html  css  js  c++  java
  • selenium-webdriver基础应用(包含登录以及系统业务处理)

    本案例是实现的功能是登录系统后添加一个部门,然后退出系统。

    1、  环境jdk1.7+selenium-server-standalone-2.49.0.jar+firefox44+myeclipse10.7

    2、  新建工程selenium,将selenium-server-standalone-2.49.0.jar导入到项目中。

    3、  新建SeleniumTest.java类。

    package com.z2sci.selenium.test;

    import java.util.Set;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;

    import org.openqa.selenium.Cookie;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.firefox.FirefoxDriver;

    public class SeleniumTest {

       private WebDriver driver;

       //登录系统

       public void login(){

       // System.setProperty("webdriver.firefox.bin", "C:\Program Files (x86)\Mozilla Firefox\firefox.exe");//如果浏览器安装目录不是系统默认的路径,则需要设置

          driver = new FirefoxDriver();

          driver.get("http://192.168.66.71:8080/dss/");

          driver.manage().window().maximize();//浏览器最大化

          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//设置窗口打开超时时间为30秒

          WebElement element = driver.findElement(By.id("password"));

          element.sendKeys("123456");//密码框输入值

          WebElement elment2 = driver.findElement(By.xpath("/html/body/div[1]/div[3]"));

          elment2.click();//点击登录按钮

          try {

            Thread.sleep(3000);//sleep一定时间

          } catch (InterruptedException e) {

            e.printStackTrace();

          }

          String currentUrl = driver.getCurrentUrl();

       if(currentUrl.equals("http://192.168.66.71:8080/dss/jsp/sysinit/sysinit.jsp")){

            Cookie cookie = new Cookie("name","value");

            driver.manage().addCookie(cookie);

          }else{

            //获得失败信息

            WebElement elmError = driver.findElement(By.xpath("/html/body/div[6]/div[2]/div[2]"));

            System.out.println("登录失败:" + elmError.getText());

          }

       }

       //添加部门

       public void addDept(){

          Set<Cookie> cookies = driver.manage().getCookies();

          for(Cookie c : cookies){

            if(c.getName().equals("name") && c.getValue().equals("value")){

               //进入到部门管理界面

               driver.get("http://192.168.66.71:8080/dss/jsp/user/dept_list.jsp");

               //定位一条数据,作为上级部门

               WebElement eleChoseDept = driver.findElement(By.id("datagrid-row-r1-2-研发部_wXG08p"));

               eleChoseDept.click();

               try {

                  Thread.sleep(1000);//sleep一定时间

               } catch (InterruptedException e) {

                  e.printStackTrace();

               }

               //点击添加同级部门按钮

               WebElement eleAddSameDept = driver.findElement(By.xpath("/html/body/div[3]/div/div/div[1]/div/form/div/a[1]"));

               eleAddSameDept.click();

               try {

                  Thread.sleep(1000);//sleep一定时间

               } catch (InterruptedException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

               }

               //添加一个部门

                WebElement eleDeptName = driver.findElement(By.xpath("/html/body/div[7]/div[2]/form/div[1]/span[1]/input[1]"));

               WebElement eleDeptDescr = driver.findElement(By.xpath("/html/body/div[7]/div[2]/form/div[2]/span/textarea"));

               eleDeptName.sendKeys("实验部323");

               eleDeptDescr.sendKeys("webdriver自动化测试");

               WebElement eleADDButton = driver.findElement(By.xpath("/html/body/div[7]/div[3]/a[1]"));

               eleADDButton.click();

               try {

                  Thread.sleep(1000);//sleep一定时间

               } catch (InterruptedException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

               }

               //获得提示框信息

               WebElement eleDigInfo = driver.findElement(By.xpath("/html/body/div[13]/div[2]/div[2]"));

               System.out.println(eleDigInfo.getText());

              

               //关闭提示框

               WebElement eleClickButton = driver.findElement(By.xpath("/html/body/div[13]/div[2]/div[4]/a"));

               System.out.println(eleClickButton.getText());

               eleClickButton.click();

              

            }

          }

       }

       //退出系统

       public void exitSys(){

       // WebElement eleADDButton = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/span[2]/a"));//根据path获得

          WebElement eleADDButton = driver.findElement(By.className("logout")); //根据class获得

          eleADDButton.click();

          driver.quit();

       }

       public static void main(String[] args) {

          SeleniumTest test = new SeleniumTest();

          test.login();

          test.addDept();

          test.exitSys();

       }

    }

  • 相关阅读:
    如何将asp.net MVC2项目升级为MVC3项目(微软官方自动升级工具:ASP.NET MVC 3 Application Upgrader )
    扩展Html Helper类,ASP.NET MVC框架提供了一个帮助我们构造Html元素的类:TagBuilder
    详解ASP.NET MVC2项目升级到MVC 3 RC
    NHibernate学习
    ASP.MVCNOTE
    MVC问题反馈页面代码
    Silverlightnote
    jqGrid
    必须掌握的八个DOS命令
    分页且带条件的存储过程
  • 原文地址:https://www.cnblogs.com/liuhaixia/p/7117641.html
Copyright © 2011-2022 走看看