美文网首页
2018-11-15

2018-11-15

作者: Kwajj | 来源:发表于2018-11-15 18:23 被阅读0次
day23java学习.png
package com.guoyasoft.autoUI.guoya_1810;

//引入 java代码路径
import com.guoyasoft.autoUI.common.BaseUI;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;


public class GuoyaLogin extends BaseUI {
  //public 公开的方法  void 无返回 login() 方法名

  //实例变量/全局变量 或者用private私有的
  public String username="guoya295";
  public String password="qweasd";
  public String realname="黄哈哈";
  public String phone="18916968152";
  public String age="25";
  public String users []={
      "ye008",
      "ye007",
      "ye006",
      "ye005",
      "ye004",
      "ye003",
      "ye002",
      "ye001",
      "ye000"};



  //添加testng 注解用来执行测试方法
  @Test
  public void login(){
    //设置循坏  起始值,最大值/最小值  增量,减量
//    for(int i=0; i<users.length; i++ ) {i

      System.out.println("当前循环次数" );
      sleep(1000);
      //打开登录网页
      //driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp");
      url("http://47.98.226.232:8080/guoya-medium/jsp/user/login.jsp");
      //线程休眠
      sleep(1000);
      //查找元素根据name查找 然后执行清除
      driver.findElement(By.name("userName")).clear();
      //查找元素根据name查找 执行输入
      driver.findElement(By.name("userName")).sendKeys(users[0]);
      //查找元素根据id查找 然后执行清除
      driver.findElement(By.id("password")).clear();
      //查找元素根据id查找 执行输入
      driver.findElement(By.id("password")).sendKeys(password);
      //查找元素根据xpath查找验证码 执行输入
      //driver.findElement(By.xpath("//input[@name='checkCode']")).sendKeys("12345");
      send("//input[@name='checkCode']","12345");
      //查找元素根据xpath查找 点击登录
      click("//input[@id='loginBtn']");
      //queryalluser();
      //调用自定义方法
      //queryuser();
      //queryage();
     //boolean 布尔类型  true真  false假
      boolean guoya =driver.getPageSource().contains("学生查询");
      //assert断言 判断预期结果与实际结果是否相等
      Assert.assertEquals(guoya,true,"用户登录页面失败");
      queryuser(users[0]);


      //切换iframe窗口至结果展示窗口
      driver.switchTo().frame("result");
      //判断切换结果展示页面是否包含查询用户
      Assert.assertEquals(driver.getPageSource().contains(users[0]),true);
      //打印新的页面代码
      System.out.println(driver.getTitle());
      //切换回默认窗口
      driver.switchTo().defaultContent();


//    }

  }

  @Test
  public void signup() {



      //最小值,最大值,增量,判断条件
      int i = 0;
      //条件成立则一直执行循环,条件不满足结束
      while (i < 10){


        //boolean result;
        //while (result=true) {

        //打开注册网页
        driver.get("http://47.98.226.232:8080/guoya-medium/jsp/user/signUp.jsp");
        sleep(1000);
        WebElement element = driver.findElement(By.id("userName"));
        element.clear();
        element.sendKeys(username);
        driver.findElement(By.id("realName")).sendKeys(realname);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("password2")).sendKeys(password);
        driver.findElement(By.id("phone")).sendKeys(phone);
        driver.findElement(By.id("age")).sendKeys(age);
        driver.findElement(By.xpath("//input[@id='checkCode']")).sendKeys("1234");
        //点击注册
        driver.findElement(By.xpath("//input[@id='submitBtn']")).click();
        //弹出弹窗 是否确定
        Alert alert = driver.switchTo().alert();
        alert.accept();
        //alert.dismiss();

        //boolean result = driver.getPageSource().contains("登录界面");
        //如果条件为真 打印注册成功
        // if (result == true) {
        //   System.out.println("用户注册成功");
        //否则就是注册失败
        // } else {
        //  System.out.println("用户注册失败");
        // }

        // result=driver.getPageSource().contains("登录界面");
        //System.out.println("注册成功");
        //}

        i++;
        System.out.println("当前循环次数" + i);
      }
    }



//全部查询
    public void queryalluser(){
      driver.findElement(By.xpath("//input[@type='submit']")).click();
      sleep(3000);
    }

    public void queryuser(String name){
      driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(name);
      driver.findElement(By.xpath("//input[@type='submit']")).click();
      sleep(3000);

    }

    public void queryage(){
      driver.findElement(By.xpath("//input[@name='userName']")).clear();
      driver.findElement(By.xpath("(//input[@type='number'])[1]")).sendKeys(age);
      driver.findElement(By.xpath("(//input[@type='submit'])")).click();
      sleep(3000);
      driver.findElement(By.xpath("//input[@name='userName']")).clear();
      driver.findElement(By.xpath("(//input[@type='number'])[1]")).sendKeys(age);
      driver.findElement(By.xpath("(//input[@type='submit'])")).click();
      sleep(3000);
    }


  public void click(String xpath){
    driver.findElement(By.xpath(xpath)) .click();
  }

  public void send(String xpath,String sendkey) {
    driver.findElement(By.xpath(xpath)).sendKeys(sendkey);
  }

  public void url(String url){
    driver.get(url);
  }

  //public void id(String id,String realname){
  // driver.findElement(By.id(id)).sendKeys(realname);
  //}


}

相关文章

网友评论

      本文标题:2018-11-15

      本文链接:https://www.haomeiwen.com/subject/rgayfqtx.html