测试工具之Selenium_模拟淘宝登录Demo
白羽 2018-12-26 来源 : 阅读 1887 评论 0

摘要:本文将带你了解测试工具Selenium_模拟淘宝登录Demo,希望本文对大家学测试工具有所帮助。

    本文将带你了解测试工具Selenium_模拟淘宝登录Demo,希望本文对大家学测试工具有所帮助。




   

package   com.lkb.start;

import   com.alibaba.fastjson.JSONObject;

import   com.lkb.bean.Entity;

import   com.lkb.bean.ResOutputTaobao;

import   com.lkb.util.ThreadUtil;

import   org.openqa.selenium.By;

import   org.openqa.selenium.WebDriver;

import   org.openqa.selenium.WebElement;

import   org.openqa.selenium.interactions.Actions;

import java.util.Scanner;

public   class TaobaoLogin {

public static void main(String[]   args) {

WebDriverUtil page = new   WebDriverUtil();

page.setManager(new   WebDriverManager());

Entity entity = new Entity();

entity.setLoginName("****");

entity.setPassword("****");

WebDriver   driver = page.get();

Actions actions = new   Actions(driver);

page.setEntity(entity);

openUrl(page);

setLoginName(page);

boolean   isShow = showSlider(page);

if (isShow)   {

slider(page);

try   {

ResOutputTaobao res =   getImgUrl(page);

System.out.println(res);

}   catch (Exception e)   {

e.printStackTrace();

}

}

//   点击坐标

System.out.println("请输入X轴坐标:");

Scanner   scanner = new Scanner(System.in);

int x =   scanner.nextInt();

System.out.println("请输入Y轴坐标:");

scanner   = new Scanner(System.in);

int y =   scanner.nextInt();

boolean isCorrect =   clickAuthCode(page,actions,x,y);

if(isCorrect){

setPassword(page);

doLogin(page);

}

System.out.println(driver.getPageSource());

System.out.println(driver.manage().getCookies());

//   driver.quit();

System.out.println("执行结束了");

}

/**

*   点击图片验证码

* @param page

* @param   actions

* @param x

* @param   y

*   @return

*/

public static boolean   clickAuthCode(WebDriverUtil page,Actions actions,int x,int   y){

WebElement element =   page.get().findElement(By.className("clickCaptcha_img")).findElement(By.tagName("img"));

actions.moveToElement(element,x,y).click().perform();

//   解析状态

return false;

}

public   static void openUrl(WebDriverUtil page)   {

page.get().get("https://login.taobao.com");

page.pageRender();

}

public   static void setLoginName(WebDriverUtil page)   {

ThreadUtil.sleep(1.1f);

WebElement   element =   page.get().findElement(By.id("TPL_username_1"));

element.clear();

element.sendKeys(page.getEntity().getLoginName());

page.pageRender();

//   切换焦点

element =   page.get().findElement(By.id("TPL_password_1"));

element.click();

ThreadUtil.sleep(1);

}

public   static void setPassword(WebDriverUtil page)   {

WebElement element =   page.get().findElement(By.id("TPL_password_1"));

element.clear();

element.sendKeys(page.getEntity().getPassword());

page.pageRender();

ThreadUtil.sleep(2);

page.pageRender();

}

public   static boolean showSlider(WebDriverUtil page)   {

WebElement element =   page.get().findElement(By.id("nocaptcha"));

//          System.out.println(element.isDisplayed());

//              ((JavascriptExecutor)page.get()).executeScript("document.getElementById(\"nocaptcha\").style.display='block'");

//              ThreadUtil.sleepMillis(300l);

return   element.isDisplayed();

}

public   static void slider(WebDriverUtil page) {

WebElement   element =   page.get().findElement(By.id("_bg"));

System.out.println(element.getLocation().getX()   + "----" +   element.getLocation().getY());

Actions action = new   Actions(page.get());

// 鼠标拖拽动作,将 source 元素拖放到 (xOffset,   yOffset) 位置,其中 xOffset 为横坐标,yOffset 为纵坐标。

int x =   element.getLocation().getX() + 1;

for (int i = 0; i   < 15; i++) {

//               action.dragAndDropBy(element,1139,430).perform();

action.dragAndDropBy(element,   x, 430).perform();

x = x + 1;

}

//           action.dragAndDropBy(element,1200,430).perform();

//           action.dragAndDropBy(element,1309,430).perform();

//           page.pageRender();

action.release();//   释放鼠标

page.pageRender();

}

/**

*   0不需要,1点击验证码,2图片验证码

*/

public   static ResOutputTaobao getImgUrl(WebDriverUtil page)   {

ResOutputTaobao obj = new   ResOutputTaobao();

String sty1 =   page.get().findElement(By.id("clickCaptcha")).getCssValue("display");

String   sty2 =   page.get().findElement(By.id("imgCaptcha")).getCssValue("display");

System.out.println(page.get().getPageSource());

if   (sty1 != null && "block".equals(sty1))   {

obj.setIsHasImg(1);

String url   =   page.get().findElement(By.className("clickCaptcha_img")).findElement(By.tagName("img")).getAttribute("src");

obj.setUrl(url);

String   text =   page.get().findElement(By.id("_scale_text")).getText();

if   (text != null) {

text =   text.replace("<i>",   "");

}

obj.setImgText(text);

}   else if (sty2 != null && "block".equals(sty2))   {

obj.setIsHasImg(2);

String url   =   page.get().findElement(By.id("_imgCaptcha_img")).findElement(By.tagName("img")).getAttribute("src");

obj.setUrl(url);

}

page.setAuthCodeMethod(obj.getIsHasImg());

return   obj;

}

public static void   clickSubmit(WebDriverUtil page, ResOutputTaobao res)   {

String value =   page.get().findElement(By.id("J_NcoToken")).getAttribute("value");

String   value1 = null;

//      long   startTime = System.currentTimeMillis();

page.get().findElement(By.id("J_SubmitStatic")).click();

WebElement   ele;

for (int i = 0; i < 100; i++)   {

ele =   page.get().findElement(By.id("J_NcoToken"));

if   (ele != null) {

value1 =   ele.getAttribute("value");

if (value1 != null   && !value1.equals(value))   {

System.out.println(page.get().getPageSource());

break;

}

ThreadUtil.sleep(400l);

}   else   {

ThreadUtil.sleep(400l);

}

}

}

public   static boolean doLogin(WebDriverUtil page) {

boolean   isNeedPhoneCode = false;

WebElement element =   page.get().findElement(By.id("J_SubmitStatic"));

element.click();

return   isNeedPhoneCode;

}

public static   void setAuthCode(WebDriverUtil page) {

String authCode   = page.getEntity().getAuthCode();

if   (page.getAuthCodeMethod() == 1) {

try   {

//淘宝传送到后台是230

JSONObject json =   JSONObject.parseObject(authCode);

float w =   json.getFloatValue("w");

float h =   json.getFloatValue("h");

float x =   json.getFloatValue("x");

float y =   json.getFloatValue("y");

float w_b = w /   230;

float h_b = h / 230;

int   x_result = (int) (x / w_b);

int y_result = (int) (y /   h_b);

WebElement webElement =   page.get().findElement(By.className("clickCaptcha_img"));

webElement   =   webElement.findElement(By.tagName("img"));

Actions   action = new   Actions(page.get());

action.moveByOffset(webElement.getLocation().x   + x_result, webElement.getLocation().y +   y_result).perform();

System.out.println((webElement.getLocation().x   + x_result) + "====" + (webElement.getLocation().y +   y_result));

System.out.println((webElement.getLocation().x)   + "====" +   (webElement.getLocation().y));

webElement.click();

action.release();//   释放鼠标

ThreadUtil.sleep(1);

page.pageRender();

//                  page.get().findElement(By.id("_btn_2")).click();

webElement   =   page.get().findElement(By.id("_btn_2"));

System.out.println((webElement.getLocation().x)   + "===" + (webElement.getLocation().y));

new   Actions(page.get()).moveToElement(webElement).perform();

webElement.click();

page.pageRender();

}   catch (Exception e)   {

e.printStackTrace();

}

}   else if (page.getAuthCodeMethod() == 2)   {

}

}

public   static boolean isCountine(WebDriverUtil page) {

boolean   b = false;

for (int i = 0; i < 10; i++)   {

if (page.getType() > 0)   {

ThreadUtil.sleep(500l);

} else   {

if (page.isLogin()) {

b =   true;

}

break;

}

}

return   b;

}

}

   


   
                   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标软件测试之测试工具频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程