当前位置: 首页 > wzjs >正文

化妆品网站开发流程和进度安排网络营销推广案例

化妆品网站开发流程和进度安排,网络营销推广案例,制作灯笼需要什么材料,投资网站模版下载文章目录 1.元素定位1.1 cssSelector(选择器)1.2 xpath1.3小示例 2.操作测试对象2.1点击/提交对象-click()2.2 模拟按键输入-sendKeys("")2.3 清除文本内容-clear()2.4 获取文本信息-getText()2.5 获取当前页面标题-getTitle()2.6获取当前页面URL-getCurrentUrl() 3.…

文章目录

  • 1.元素定位
    • 1.1 cssSelector(选择器)
    • 1.2 xpath
    • 1.3小示例
  • 2.操作测试对象
    • 2.1点击/提交对象-click()
    • 2.2 模拟按键输入-sendKeys("")
    • 2.3 清除文本内容-clear()
    • 2.4 获取文本信息-getText()
    • 2.5 获取当前页面标题-getTitle()
    • 2.6获取当前页面URL-getCurrentUrl()
  • 3.窗口
    • 3.1 切换窗口
    • 3.2 窗口设置大小
    • 3.3 屏幕截图
    • 3.4关闭窗口

1.元素定位

web自动化测试的操作核心是能够找到页面对应的元素。

如何手动在网页页面中查找元素

  1. 打开开发者工具
    方法一:右键---->检查
    方法二:ctrl + shift + i
    方法三:F12
    在这里插入图片描述

  2. 点击右上角的箭头
    在这里插入图片描述

  3. 例如找搜索框对应的元素
    在这里插入图片描述
    在web自动化测试中常见的元素定位方式非常多,如id,classname,tagname,xpath,cssSelector。常用的主要由cssSelector和xpath

1.1 cssSelector(选择器)

功能:选中页面中指定的标签元素
选择器的种类分为基础选择器和复合选择器常见的元素定位方式可以通过id选择器和子类选择器来
进行定位。

  1. id选择器
    功能:选取某个id的元素
    格式:#id
    例如:百度搜索框的id选择器,该元素的id为kw,id选择器为#kw
    在这里插入图片描述
    在这里插入图片描述
  2. 子类选择器
    功能:选中某个元素下的直接子元素。
    格式:div > p
    例如:寻找百度热搜元素
    在这里插入图片描述
    复制到搜索框:#s-hotsearch-wrapper > div > a.hot-title > div
    在这里插入图片描述
    #s-hotsearch-wrapper > div > a.hot-title > div ==> 往#s-hotsearch-wrapper的子类里面找
    在这里插入图片描述

1.2 xpath

XML路径语言,不仅可以在XML文件中查找信息,还可以在HTML页面中选取元素。
在这里插入图片描述

语法:
1.2.1 获取HTML页面所有的节点

//*

1.2.2 获取HTML页面指定的节点

//[指定节点]
//ul :获取HTML⻚⾯所有的ul节点
//input:获取HTML⻚⾯所有的input节点

1.2.3 获取⼀个节点中的直接子节点

/
//span/input

1.2.4 获取⼀个节点的父节点

..
//input/.. 获取input节点的⽗节点

1.2.5 实现节点属性的匹配

[@...]
//*[@id='kw'] 匹配HTML⻚⾯中id属性为kw的节点

1.2.6 使用指定索引的方式获取对应的节点内容

注意:xpath的索引是从1开始的。
百度首页通过://div/ul/li[3] 定位到第三个百度热搜标签

1.3小示例

使用选择器和xpath定位百度热搜,并查找所有的热搜词条

方法名作用返回值
findElement(By)在页面中查找元素WebElement
findElements(By)在页面中查找多个元素List< WebElement >
public class FirstAutoTest {WebDriver driver = null;void createDriver() {// 1.打开谷歌浏览器WebDriverManager.chromedriver().setup();// 浏览器配置:允许访问所有链接ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);// 2.输入网址:https://www.baidu.comdriver.get("https://www.baidu.com");}// 测试元素定位void test02() {createDriver();// 选择器driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div"));// xpathdriver.findElement(By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/div/a[1]/div"));// 查找多个热搜词条List<WebElement> list = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));for (WebElement x:list) {System.out.println(x.getText());}driver.quit();}public static void main(String[] args) throws InterruptedException {FirstAutoTest test = new FirstAutoTest();test.test02();}
}

运行结果
在这里插入图片描述

2.操作测试对象

获取到了元素,我们便可以对元素进行操作了。
常见的操作有点击、提交、输入、清除、获取文本。

2.1点击/提交对象-click()

click()
//找到百度⼀下按钮并点击
driver.findElement(By.cssSelector("#su")).click();

注意: 页面隐藏的标签,不可见的标签不能点击。

2.2 模拟按键输入-sendKeys(“”)

sendKeys("")
// 在百度搜索框中输入“CSDN”
driver.findElement(By.cssSelector("#kw")).sendKeys("CSDN");

2.3 清除文本内容-clear()

clear()
driver.findElement(By.cssSelector("#kw")).sendKeys("玩游戏");
driver.findElement(By.cssSelector("#kw")).clear(); // 清空搜索框
driver.findElement(By.cssSelector("#kw")).sendKeys("写作业");

2.4 获取文本信息-getText()

getText()
// 查找并打印热搜词条List<WebElement> list = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));for (WebElement x : list) {System.out.println(x.getText());}

注意:文本和属性值不要混淆了。获取属性值需要使用方法 getAttribute(“属性名称”) ;

2.5 获取当前页面标题-getTitle()

getTitle()

2.6获取当前页面URL-getCurrentUrl()

getCurrentUrl()

举例

String title = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
System.out.println("title = " + title);
System.out.println("Url = " + currentUrl);

在这里插入图片描述

3.窗口

我们可以通过眼睛来判断当前的窗口是哪一个。对于程序而言,每个浏览器窗口的唯⼀的属性==句柄(handle)==就是它的眼睛。我们可以通过句柄来切换窗口。
在这里插入图片描述

3.1 切换窗口

1)获取当前页面句柄:

driver.getWindowHandle();

3)获取所有页面句柄:

driver.getWindowHandles();

3)切换当前句柄为最新页面

String curWindow = driver.getWindowHandle();
Set<String> allWindow = driver.getWindowHandles();
for( String w : allWindow){if(w!=curWindow){driver.switchTo().window(w);}
}

3.2 窗口设置大小

//窗⼝最⼤化
driver.manage().window().maximize();
//窗⼝最⼩化
driver.manage().window().minimize();
//全屏窗⼝
driver.manage().window().fullscreen();
//⼿动设置窗⼝⼤⼩
driver.manage().window().setSize(new Dimension(1024, 768));

3.3 屏幕截图

如果程序出现了报错,我们可以通过抓拍来记录当时的错误场景。
要使用FileUtils类,先在pom.xml中额外导入包

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>
  1. 低级屏幕截图:
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("test.png"));

在这里插入图片描述
2. 高级屏幕截图
将图片保存到:./src/test/image/年月日/方法名-时分秒毫秒.png

public class FirstAutoTest {WebDriver driver = null;void createDriver() {// 1.打开谷歌浏览器WebDriverManager.chromedriver().setup();// 浏览器配置:允许访问所有链接ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);// 2.输入网址:https://www.baidu.comdriver.get("https://www.baidu.com");}// 高级屏幕截图void getScreenShot(String str) throws IOException {// 截屏时间SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS"); // 精确到毫秒String dirTime = sim1.format(System.currentTimeMillis());String fileTime = sim2.format(System.currentTimeMillis());// ./src/test/image/年月日/方法名-时分秒毫秒.pngString fileName = "./src/test/image/" + dirTime + "/" + str + "-" + fileTime + ".png";System.out.println("fileName: " + fileName);File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(file, new File(fileName));}void test05() throws IOException {createDriver();getScreenShot(getClass().getName());getScreenShot(getClass().getName());}public static void main(String[] args) throws InterruptedException, IOException {FirstAutoTest test = new FirstAutoTest();test.test05();}
}

结果
在这里插入图片描述

3.4关闭窗口

driver.close(); // 关闭当前的标签页 
// 窗⼝关闭后driver要重新定义

文章转载自:

http://OdOauHEv.pxdjL.cn
http://Bu9emopM.pxdjL.cn
http://SUaz55B9.pxdjL.cn
http://z3PuMSvk.pxdjL.cn
http://kEkQ1cZG.pxdjL.cn
http://STUFyseA.pxdjL.cn
http://n0wR2F8W.pxdjL.cn
http://Miwnkqr1.pxdjL.cn
http://8ikbfiTN.pxdjL.cn
http://jfqmKXZ6.pxdjL.cn
http://MONK7zAo.pxdjL.cn
http://Tqb60Tml.pxdjL.cn
http://BnddFGDt.pxdjL.cn
http://uZ8rf9H3.pxdjL.cn
http://t5lchqMN.pxdjL.cn
http://xK0aDH1C.pxdjL.cn
http://NK1JKYlE.pxdjL.cn
http://GphXjXJm.pxdjL.cn
http://fXkvKWMY.pxdjL.cn
http://nSWYMzf0.pxdjL.cn
http://mJSKsLZt.pxdjL.cn
http://EuUgWtsq.pxdjL.cn
http://gup4vUdb.pxdjL.cn
http://qHfrTtG2.pxdjL.cn
http://M3QYMnNu.pxdjL.cn
http://DmOHFOlw.pxdjL.cn
http://ID0HcR6D.pxdjL.cn
http://7K5wOWw8.pxdjL.cn
http://0Olvyhqv.pxdjL.cn
http://tqM1jPKw.pxdjL.cn
http://www.dtcms.com/wzjs/647345.html

相关文章:

  • 网站站点查询郑州网站建设开拓者
  • 合肥网站维护公司淘宝客建站模板
  • 新手写作网站wordpress编辑文章
  • 深圳网站系统建设wordpress 生成gif
  • 厦门旅游网站建设目的影视动漫专业
  • 深圳哪些设计公司做网站比较出名显示官网字样的网站怎么做
  • 华为公司网站建设方案模板博物馆网站建设的目标
  • 专业网站建设的意义上海做推广的引流公司
  • 卓老师建站网站后台如何直接登陆济南大型网站建设
  • 门户网站建设计划软件开发一个月多少钱
  • 论坛型网站建设新闻今天
  • 帝国cms网站名称wordpress最好选择
  • 贵阳网站建设咨询微盟商户助手app下载
  • 网站seo自己怎么做巡视组 住房与城乡建设部网站
  • 珠海美容网站建设软文网官网
  • 南沙区交通和建设局网站互联网架构师
  • wordpress建站位置福州网站建设的公司
  • 1.简述网站建设流程工商注册号查询入口
  • 网站不收录是什么原因wordpress中数据库配置文件
  • 找施工队伍去什么网站国内优秀企业网站欣赏
  • 苏州哪里做网站手机大型网站
  • asp.net+网站开发+实战怎么免费创建自己的网站平台
  • 网站页面的宽度郑州装修设计公司
  • 网站查询功能是用什么程序做的传媒公司取名字
  • 悬赏做logo的网站重庆网站界面设计
  • 石家庄最好的网站建设公司排名广告设计学什么内容
  • 银州手机网站建设网站建设综合实训总结与体会
  • 网站建设涉及的法律做竞价网站要准备什么条件
  • 网站建设 经典书籍优惠的网站快排公司电话
  • 烟台营销型网站建设成品网站灬源码1688