提交 8f1f2169 编写于 作者: W wizardforcel

2020-06-24 14:53:52

上级 c1c4f675
...@@ -8,15 +8,15 @@ ...@@ -8,15 +8,15 @@
1. 为每个会话生成唯一的 ID(带有随机字母数字字符) 1. 为每个会话生成唯一的 ID(带有随机字母数字字符)
2. 各种元素的重复类名 2. 各种元素的重复类名
3. 多个 div 标签使 XPath 的长度比平时更长 3. 多个`div`标签使 XPath 的长度比平时更长
所有这些使自动化测试人员的工作更具挑战性! 绝对 XPath 可能会有所帮助,但是如果 DOM 中包含新元素或现有元素稍微移动了一点,那么脚本可能会失败,因为找不到该元素。 相对的 XPath 可能可以解决,但是即使那样,由于脚本变得复杂,整个过程也变得很困难。 所有这些使自动化测试人员的工作更具挑战性! 绝对 XPath 可能会有所帮助,但是如果 DOM 中包含新元素或现有元素稍微移动了一点,那么脚本可能会失败,因为找不到该元素。 相对的 XPath 可能可以解决,但是即使那样,由于脚本变得复杂,整个过程也变得很困难。
The easy and best way to handle such situations is to introduce **`data-* attributes`** (custom data attributes) on HTML elements. The only catch here is, you will need to be in the good books of the developer! 😉 He/she is the one to add these attributes to the code as per your requirement. 处理此类情况的最简单便捷的方法是在HTML元素上引入`data-*`属性(自定义数据属性)。 这里唯一的问题是,您将需要成为开发人员的好书! 他/她是根据您的要求将这些属性添加到代码中的人。😉
现在让我对这些自定义数据属性进行学习, 现在让我对这些自定义数据属性进行学习,
custom 属性允许我们在 HTML 元素上存储额外的数据或信息。 要遵循的规则是, `custom`属性允许我们在 HTML 元素上存储额外的数据或信息。 要遵循的规则是,
1. 属性名称应以`data-`开头 1. 属性名称应以`data-`开头
2.`data-`的此前缀之后,属性名称应至少一个字符长,并且只能使用小写字母 2.`data-`的此前缀之后,属性名称应至少一个字符长,并且只能使用小写字母
......
...@@ -12,16 +12,16 @@ ...@@ -12,16 +12,16 @@
创建可执行文件意味着 创建可执行文件意味着
* src 文件夹下编译所需的.java 文件 *`src`文件夹下编译所需的`.java`文件
* 为 JAR 创建清单文件 * 为 JAR 创建清单文件
* 将所有.class 文件和清单文件一起压缩 * 将所有`.class`文件和清单文件一起压缩
* 使用 jar 命令创建可执行文件 * 使用 jar 命令创建可执行文件
但是,使用 Apache ANT 可以通过简单的目标处理所有这些步骤(我们将在一分钟内看到 Target 是什么!)。 但是,使用 Apache ANT 可以通过简单的目标处理所有这些步骤(我们将在一分钟内看到 Target 是什么!)。
要完成的所有工作都以结构化和模块化的方式在 XML 文件中指定,从而很容易识别是否有问题。 这个文件被称为著名的“`build.xml`”。 要完成的所有工作都以结构化和模块化的方式在 XML 文件中指定,从而很容易识别是否有问题。 这个文件被称为著名的“`build.xml`”。
Apache ANT 带有一个名为 build.xml 的默认构建文件。 我们总是可以编辑或创建一个新的 XML 文件来满足我们的需求。 这基本上是**命令行工具**。 因此,使用命令“ant build_file_name.xml”从命令提示符运行 Apache ANT 非常容易。 最好的部分是,如果您没有重命名 build.xml(只需修改提供的默认值或创建您自己的名称并将其命名为 build.xml),那么只有命令“ant”会自动知道要查找构建。 xml 并执行它的作用! Apache ANT 带有一个名为`build.xml`的默认构建文件。 我们总是可以编辑或创建一个新的 XML 文件来满足我们的需求。 这基本上是**命令行工具**。 因此,使用命令“`ant build_file_name.xml`”从命令提示符运行 Apache ANT 非常容易。 最好的部分是,如果您没有重命名`build.xml`(只需修改提供的默认值或创建您自己的名称并将其命名为`build.xml`),那么只有命令“`ant`”会自动知道要查找`build.xml`并执行它的作用!
Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们可以, Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们可以,
...@@ -39,9 +39,9 @@ Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们 ...@@ -39,9 +39,9 @@ Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们
*还有很多其他事情…* *还有很多其他事情…*
听起来很吓人,但到目前为止,我刚才提到的每件事都可以通过 buildfile 来实现。 听起来很吓人,但到目前为止,我刚才提到的每件事都可以通过构建文件来实现。
是时候忙于大肆宣传的 buildfile 的元素了。 这些都是用 XML 编写的。 是时候忙于大肆宣传的构建文件的元素了。 这些都是用 XML 编写的。
**注意***以下信息足以使您熟悉 Apache ANT 构建文件。 有关深入的知识,请访问 Apache ANT 用户手册(<http://ant.apache.org/manual/>)。* **注意***以下信息足以使您熟悉 Apache ANT 构建文件。 有关深入的知识,请访问 Apache ANT 用户手册(<http://ant.apache.org/manual/>)。*
...@@ -51,7 +51,7 @@ Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们 ...@@ -51,7 +51,7 @@ Apache ANT 具有强大的功能,足以超越“谢谢”的门槛! 我们
2. 目标 – 执行工作单元的一组任务 2. 目标 – 执行工作单元的一组任务
3. 任务 – 可以执行的一段代码 3. 任务 – 可以执行的一段代码
示例 build.xml 文件如下, 示例`build.xml`文件如下,
![sample build.xml](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20839%20425'%3E%3C/svg%3E) ![sample build.xml](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20839%20425'%3E%3C/svg%3E)
......
...@@ -6,12 +6,11 @@ ...@@ -6,12 +6,11 @@
因此,今天,我们将使用 Apache ANT 的 junitreport 任务来生成一个任务。 准备点亮! 因此,今天,我们将使用 Apache ANT 的 junitreport 任务来生成一个任务。 准备点亮!
从我的[上一篇文章](https://javabeginnerstutorial.com/selenium/10a-advanced-webdriver-apache-ant/)中,我们生成了带有“junit”作为默认 JUnit 输出目录的构建文件。 用简单的英语来说,这仅意味着将作为 JUnit 报告任务的一部分生成的所有文件都放置在名为“junit”的目录下。 从我的[上一篇文章](https://javabeginnerstutorial.com/selenium/10a-advanced-webdriver-apache-ant/)中,我们生成了带有“`junit`”作为默认 JUnit 输出目录的构建文件。 用简单的英语来说,这仅意味着将作为 JUnit 报告任务的一部分生成的所有文件都放置在名为“`junit`”的目录下。
#### 步骤 1 : #### 步骤 1 :
在项目“Selenium”下,将生成名为“build.xml”的 ANT 构建文件。 我已经在“com.blog.junitTests”包下创建了两个 JUnit 测试用例,分别是 [RadioBtns_Checkboxes.java](https://javabeginnerstutorial.com/selenium/9t-webdriver-handling-radio-buttons-checkboxes/)[SelectItems.java](https://javabeginnerstutorial.com/selenium/9u-webdriver-select-items-two-ways/) ,如下图所示。 该代码可在相应的帖子中找到。 只需单击文件名即可进行导航。 在项目“Selenium”下,将生成名为“`build.xml`”的 ANT 构建文件。 我已经在“`com.blog.junitTests`”包下创建了两个 JUnit 测试用例,分别是 [RadioBtns_Checkboxes.java](https://javabeginnerstutorial.com/selenium/9t-webdriver-handling-radio-buttons-checkboxes/)[SelectItems.java](https://javabeginnerstutorial.com/selenium/9u-webdriver-select-items-two-ways/) ,如下图所示。 该代码可在相应的帖子中找到。 只需单击文件名即可进行导航。
![Package structure](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20354%20273'%3E%3C/svg%3E)
<noscript><img alt="Package structure" class="alignnone size-full wp-image-13022" height="273" src="img/bbbccea0a3928f2e18e1c51e8230d1e6.png" width="354"/><h4><strong>步骤 2 </strong></h4><p>打开“build.xml”,并确保自动生成“junitreport”任务。 以下是我的项目的目标(初始化,清理,构建,RadioBtns_Checkboxes,SelectItems 和 junitreport)。</p><pre><code class="language-xml">&lt;project basedir="." default="build" name="Selenium"&gt; &lt;property environment="env"/&gt; &lt;property name="junit.output.dir" value="junit"/&gt; &lt;property name="debuglevel" value="source,lines,vars"/&gt; &lt;property name="target" value="1.8"/&gt; &lt;property name="source" value="1.8"/&gt; &lt;target name="init"&gt; &lt;mkdir dir="bin"/&gt; &lt;copy includeemptydirs="false" todir="bin"&gt; &lt;fileset dir="src"&gt; &lt;exclude name="**/*.java"/&gt; &lt;/fileset&gt; &lt;/copy&gt; &lt;/target&gt; &lt;target name="clean"&gt; &lt;delete dir="bin"/&gt; &lt;/target&gt; &lt;target name="build" depends="init"&gt; &lt;echo message="${ant.project.name}: ${ant.file}"/&gt; &lt;javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}"&gt; &lt;src path="src"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/javac&gt; &lt;/target&gt; &lt;target name="RadioBtns_Checkboxes"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.RadioBtns_Checkboxes" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="SelectItems"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.SelectItems" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="junitreport"&gt; &lt;junitreport todir="${junit.output.dir}"&gt; &lt;fileset dir="${junit.output.dir}"&gt; &lt;include name="TEST-*.xml"/&gt; &lt;/fileset&gt; &lt;report format="frames" todir="${junit.output.dir}"/&gt; &lt;/junitreport&gt; &lt;/target&gt; &lt;/project&gt;</code></pre><h4><strong>步骤 3 </strong></h4><p>可以通过以下任意一种方式执行构建,</p><p>1.右键单击构建文件(build.xml)-&gt;运行方式-&gt; Ant Build</p><p><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/9_RepRunMethod1-1.jpg" height="416" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20775%20416'%3E%3C/svg%3E" width="775"/></p><noscript><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" height="416" src="img/1f57bab2e596907cfafba4a0f756ff86.png" width="775"/><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad ezoic-adl" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span> 2.右键单击构建文件(build.xml)-&gt;运行方式-&gt;外部工具配置-&gt;运行</p><p>3.蚀工具栏中的快捷方式运行图标</p><p><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" data-lazy-sizes="(max-width: 536px) 100vw, 536px" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg" data-lazy-srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" height="201" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20536%20201'%3E%3C/svg%3E" width="536"/></p><noscript><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" height="201" sizes="(max-width: 536px) 100vw, 536px" src="img/6a5b08798471e00aeee6fc71c824d310.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" width="536"/><p>让我们采用第二种方法,看看如何使用“外部工具配置”选项修改现有配置。</p><p>因此,右键单击构建文件-&gt;运行方式-&gt;外部工具配置:这将打开一个弹出窗口。 选择“主要”标签,并确保选择了正确的构建文件。</p><p><img alt="Main Configuration" class="alignnone size-full wp-image-13023" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/7_RepMainConfig-1.jpg" height="248" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20783%20248'%3E%3C/svg%3E" width="783"/></p><noscript><img alt="Main Configuration" class="alignnone size-full wp-image-13023" height="248" src="img/4fa275eb64b4fb422b00608b496fe7f8.png" width="783"/><p>然后点击“目标”标签。</p><ol><li>将列出构建文件中定义的所有目标名称,并且仅检查默认目标。 点击“运行”,选择要执行的所有目标。</li><li>在“目标执行顺序”框中,将从上一步中选择的所有目标按执行顺序列出。</li><li>如果您想更改订单,请点击“订单...”。 这将打开“订单目标”弹出窗口。</li><li>我们可以通过选择目标并相应地单击“上”或“下”按钮来在此弹出窗口中指定目标执行顺序。 点击“确定”以确认最终的执行顺序。</li><li>完成所有操作后,点击“应用”和“运行”</li></ol><p><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/8_RepTargetConfig.jpg" height="591" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20845%20591'%3E%3C/svg%3E" width="845"/></p><noscript><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" height="591" src="img/8af13d9bb45d3942d6d51b53f17f884f.png" width="845"/><h4><strong>步骤 4 </strong></h4><p>可以通过查看控制台视图来验证成功执行。 它显示了按配置顺序执行的所有目标以及基于构建文件中提到的日志记录级别的日志。 如图所示,将显示运行构建所花费的总时间以及消息“BUILD SUCCESSFUL”或“BUILD FAILED”。</p><p><img alt="Console Output" class="alignnone size-full wp-image-13019" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/11_RepConsoleOutput.jpg" height="515" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20790%20515'%3E%3C/svg%3E" width="790"/></p><noscript><img alt="Console Output" class="alignnone size-full wp-image-13019" height="515" src="img/0fb58b99d8612d1dccb306cceb89eba4.png" width="790"/><h4><strong>步骤 5 </strong></h4><p>在 Eclipse 的“Package Explorer”视图中,右键单击该项目,然后单击“Refresh”或 F5。 已经创建了“junit”文件夹(在生成构建文件时在“JUnit 输出目录”框中指定的名称)。 这是 JUnit 报告(index.html)和 XML 一起提供的地方,用于执行的每个测试用例,显示其成功或失败。</p><p><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/12_RepJUnitFolder-1.jpg" height="402" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20646%20402'%3E%3C/svg%3E" width="646"/></p><noscript><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" height="402" src="img/c5e6944f046312e1960f0c4919875688.png" width="646"/><p>或者,在文件浏览器中导航到项目路径,然后双击“junit”文件夹。</p><h4><strong>步骤 6 </strong></h4><p>在浏览器中打开“<strong> index.html </strong>”,并检查生成的默认报告。 这将在左窗格中显示所涉及的包和所有已执行的 Java 文件。</p><p>右窗格显示测试结果的摘要,其中包括测试的总数,失败,错误,跳过,成功率和执行时间。 还显示了包列表,其中包含类似的详细信息。</p><p>要深入查看所有已执行的测试或失败等,请单击显示的数字,如下图所示,</p><p><img alt="Generated report" class="alignnone size-full wp-image-13021" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/13_ReportGenerated.jpg" height="436" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20761%20436'%3E%3C/svg%3E" width="761"/></p><noscript><img alt="Generated report" class="alignnone size-full wp-image-13021" height="436" src="img/f6fcd46485176dabfbaa70e1208db3aa.png" width="761"/><p>用 Apache ANT 生成 JUnit 报告不是一件容易的事吗? 猜猜这就是我们今天要做的。</p><p>在另一篇文章中再见。 祝您报告愉快!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/" title="10c. Advanced WebDriver – JUnit Report Customization"> 10c。 高级 WebDriver – JUnit 报表自定义</a></h5></div></div> </body> </html></noscript> <noscript><img alt="Package structure" class="alignnone size-full wp-image-13022" height="273" src="img/bbbccea0a3928f2e18e1c51e8230d1e6.png" width="354"/><h4><strong>步骤 2 </strong></h4><p>打开“build.xml”,并确保自动生成“junitreport”任务。 以下是我的项目的目标(初始化,清理,构建,RadioBtns_Checkboxes,SelectItems 和 junitreport)。</p><pre><code class="language-xml">&lt;project basedir="." default="build" name="Selenium"&gt; &lt;property environment="env"/&gt; &lt;property name="junit.output.dir" value="junit"/&gt; &lt;property name="debuglevel" value="source,lines,vars"/&gt; &lt;property name="target" value="1.8"/&gt; &lt;property name="source" value="1.8"/&gt; &lt;target name="init"&gt; &lt;mkdir dir="bin"/&gt; &lt;copy includeemptydirs="false" todir="bin"&gt; &lt;fileset dir="src"&gt; &lt;exclude name="**/*.java"/&gt; &lt;/fileset&gt; &lt;/copy&gt; &lt;/target&gt; &lt;target name="clean"&gt; &lt;delete dir="bin"/&gt; &lt;/target&gt; &lt;target name="build" depends="init"&gt; &lt;echo message="${ant.project.name}: ${ant.file}"/&gt; &lt;javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}"&gt; &lt;src path="src"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/javac&gt; &lt;/target&gt; &lt;target name="RadioBtns_Checkboxes"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.RadioBtns_Checkboxes" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="SelectItems"&gt; &lt;mkdir dir="${junit.output.dir}"/&gt; &lt;junit fork="yes" printsummary="withOutAndErr"&gt; &lt;formatter type="xml"/&gt; &lt;test name="com.blog.junitTests.SelectItems" todir="${junit.output.dir}"/&gt; &lt;classpath refid="Selenium.classpath"/&gt; &lt;/junit&gt; &lt;/target&gt; &lt;target name="junitreport"&gt; &lt;junitreport todir="${junit.output.dir}"&gt; &lt;fileset dir="${junit.output.dir}"&gt; &lt;include name="TEST-*.xml"/&gt; &lt;/fileset&gt; &lt;report format="frames" todir="${junit.output.dir}"/&gt; &lt;/junitreport&gt; &lt;/target&gt; &lt;/project&gt;</code></pre><h4><strong>步骤 3 </strong></h4><p>可以通过以下任意一种方式执行构建,</p><p>1.右键单击构建文件(build.xml)-&gt;运行方式-&gt; Ant Build</p><p><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/9_RepRunMethod1-1.jpg" height="416" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20775%20416'%3E%3C/svg%3E" width="775"/></p><noscript><img alt="Report Generation Method 1" class="alignnone size-full wp-image-13025" height="416" src="img/1f57bab2e596907cfafba4a0f756ff86.png" width="775"/><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad ezoic-adl" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span> 2.右键单击构建文件(build.xml)-&gt;运行方式-&gt;外部工具配置-&gt;运行</p><p>3.蚀工具栏中的快捷方式运行图标</p><p><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" data-lazy-sizes="(max-width: 536px) 100vw, 536px" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg" data-lazy-srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" height="201" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20536%20201'%3E%3C/svg%3E" width="536"/></p><noscript><img alt="Report Generation Method 2" class="alignnone size-full wp-image-13018" height="201" sizes="(max-width: 536px) 100vw, 536px" src="img/6a5b08798471e00aeee6fc71c824d310.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2.jpg 536w, https://javabeginnerstutorial.com/wp-content/uploads/2018/05/10_RepRunMethod2-300x113.jpg 300w" width="536"/><p>让我们采用第二种方法,看看如何使用“外部工具配置”选项修改现有配置。</p><p>因此,右键单击构建文件-&gt;运行方式-&gt;外部工具配置:这将打开一个弹出窗口。 选择“主要”标签,并确保选择了正确的构建文件。</p><p><img alt="Main Configuration" class="alignnone size-full wp-image-13023" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/7_RepMainConfig-1.jpg" height="248" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20783%20248'%3E%3C/svg%3E" width="783"/></p><noscript><img alt="Main Configuration" class="alignnone size-full wp-image-13023" height="248" src="img/4fa275eb64b4fb422b00608b496fe7f8.png" width="783"/><p>然后点击“目标”标签。</p><ol><li>将列出构建文件中定义的所有目标名称,并且仅检查默认目标。 点击“运行”,选择要执行的所有目标。</li><li>在“目标执行顺序”框中,将从上一步中选择的所有目标按执行顺序列出。</li><li>如果您想更改订单,请点击“订单...”。 这将打开“订单目标”弹出窗口。</li><li>我们可以通过选择目标并相应地单击“上”或“下”按钮来在此弹出窗口中指定目标执行顺序。 点击“确定”以确认最终的执行顺序。</li><li>完成所有操作后,点击“应用”和“运行”</li></ol><p><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/8_RepTargetConfig.jpg" height="591" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20845%20591'%3E%3C/svg%3E" width="845"/></p><noscript><img alt="ANT target configuration" class="alignnone size-full wp-image-13024" height="591" src="img/8af13d9bb45d3942d6d51b53f17f884f.png" width="845"/><h4><strong>步骤 4 </strong></h4><p>可以通过查看控制台视图来验证成功执行。 它显示了按配置顺序执行的所有目标以及基于构建文件中提到的日志记录级别的日志。 如图所示,将显示运行构建所花费的总时间以及消息“BUILD SUCCESSFUL”或“BUILD FAILED”。</p><p><img alt="Console Output" class="alignnone size-full wp-image-13019" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/11_RepConsoleOutput.jpg" height="515" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20790%20515'%3E%3C/svg%3E" width="790"/></p><noscript><img alt="Console Output" class="alignnone size-full wp-image-13019" height="515" src="img/0fb58b99d8612d1dccb306cceb89eba4.png" width="790"/><h4><strong>步骤 5 </strong></h4><p>在 Eclipse 的“Package Explorer”视图中,右键单击该项目,然后单击“Refresh”或 F5。 已经创建了“junit”文件夹(在生成构建文件时在“JUnit 输出目录”框中指定的名称)。 这是 JUnit 报告(index.html)和 XML 一起提供的地方,用于执行的每个测试用例,显示其成功或失败。</p><p><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/12_RepJUnitFolder-1.jpg" height="402" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20646%20402'%3E%3C/svg%3E" width="646"/></p><noscript><img alt="Report in JUnit folder" class="alignnone size-full wp-image-13020" height="402" src="img/c5e6944f046312e1960f0c4919875688.png" width="646"/><p>或者,在文件浏览器中导航到项目路径,然后双击“junit”文件夹。</p><h4><strong>步骤 6 </strong></h4><p>在浏览器中打开“<strong> index.html </strong>”,并检查生成的默认报告。 这将在左窗格中显示所涉及的包和所有已执行的 Java 文件。</p><p>右窗格显示测试结果的摘要,其中包括测试的总数,失败,错误,跳过,成功率和执行时间。 还显示了包列表,其中包含类似的详细信息。</p><p>要深入查看所有已执行的测试或失败等,请单击显示的数字,如下图所示,</p><p><img alt="Generated report" class="alignnone size-full wp-image-13021" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/05/13_ReportGenerated.jpg" height="436" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20761%20436'%3E%3C/svg%3E" width="761"/></p><noscript><img alt="Generated report" class="alignnone size-full wp-image-13021" height="436" src="img/f6fcd46485176dabfbaa70e1208db3aa.png" width="761"/><p>用 Apache ANT 生成 JUnit 报告不是一件容易的事吗? 猜猜这就是我们今天要做的。</p><p>在另一篇文章中再见。 祝您报告愉快!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/" title="10c. Advanced WebDriver – JUnit Report Customization"> 10c。 高级 WebDriver – JUnit 报表自定义</a></h5></div></div> </body> </html></noscript>
\ No newline at end of file
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
> 原文: [https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/](https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/) > 原文: [https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/](https://javabeginnerstutorial.com/selenium/10c-advanced-webdriver-junit-report-customization/)
自定义! 谁不喜欢自己作品中的某些独特之处? 让我们在[上添加一些糖霜,Apache ANT 生成的 JUnit 报告](https://javabeginnerstutorial.com/selenium/10b-advanced-webdriver-generating-junit-reports/)通过引入一些优点使其看起来更漂亮。 自定义! 谁不喜欢自己作品中的某些独特之处? 让我们在[Apache ANT 生成的 JUnit 报告上添加一些糖霜](https://javabeginnerstutorial.com/selenium/10b-advanced-webdriver-generating-junit-reports/)通过引入一些优点使其看起来更漂亮。
作为自定义自动生成报告的第一步,让我们了解其背后的代码(来自 Build.xml), 作为自定义自动生成报告的第一步,让我们了解其背后的代码(来自`Build.xml`),
```java ```java
<junitreport todir="junit"> <junitreport todir="junit">
...@@ -17,13 +17,13 @@ ...@@ -17,13 +17,13 @@
Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告, Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告,
* JUnit 任务生成的所有单个 XML 文件都被合并并命名为 TESTS-TestSuites.xml * JUnit 任务生成的所有单个 XML 文件都被合并并命名为`TESTS-TestSuites.xml`
* CSS 样式应用于合并后的文档 * CSS 样式应用于合并后的文档
* 最终的可浏览报告以 HTML 格式提供 * 最终的可浏览报告以 HTML 格式提供
`<junitreport todir="junit">` `<junitreport todir="junit">`
属性“todir”指定了将合并的 XML 文件保存到的目录。 如果未指定任何内容,则默认为当前目录。 属性“`todir`”指定了将合并的 XML 文件保存到的目录。 如果未指定任何内容,则默认为当前目录。
```java ```java
<fileset dir="junit"> <fileset dir="junit">
...@@ -31,29 +31,29 @@ Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告, ...@@ -31,29 +31,29 @@ Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告,
</fileset> </fileset>
``` ```
**注意***所有单独的 JUnit 测试都是通过“junit” ANT 任务执行的,生成的 XML 文件为 生成的名称使用“outfile”属性指定。 如果未提及名称,则默认值为“TEST-name”(其中 name 是在 junit task 的 name 属性中指定的测试名称)。* **注意***所有单独的 JUnit 测试都是通过“junit” ANT 任务执行的,生成的 XML 文件为 生成的名称使用“`outfile`”属性指定。 如果未提及名称,则默认值为“`TEST-name`”(其中`name`是在 junit 任务的`name`属性中指定的测试名称)。*
使用<文件集>元素,junitreport 会从“dir”中提到的给定目录中收集所有单个 XML 文件,并根据上述代码段考虑所有以“TEST-”开头的文件。 使用`fileset`元素,junitreport 会从“`dir`”中提到的给定目录中收集所有单个 XML 文件,并根据上述代码段考虑所有以“`TEST-`”开头的文件。
`<report format="frames" todir="junit/report"/>` `<report format="frames" todir="junit/report"/>`
这是从合并文件生成可浏览报告的确切任务。 这是从合并文件生成可浏览报告的确切任务。
属性“格式”指定可用于生成报告的两种可用格式之一。 它们必须是“框架”或“无框架”。 如果未指定任何内容,则默认为“帧”格式。 “todir”属性指定生成的报告必须保存到的目录。 属性“格式”指定可用于生成报告的两种可用格式之一。 它们必须是“`frames`”或“`noframes`”。 如果未指定任何内容,则默认为“帧”格式。 “`todir`”属性指定生成的报告必须保存到的目录。
*`frames`”格式将生成一个报告,该报告将包含具有重定向功能的多个文件,并使用样式表来实现此效果。 这是一种更具可读性的格式。 *`frames`”格式将生成一个报告,该报告将包含具有重定向功能的多个文件,并使用样式表来实现此效果。 这是一种更具可读性的格式。
*`noframes`”格式会生成一个名为“junit-noframes.html”的文件,没有任何框架,并且不使用任何类型的重定向。 这种类型更适合作为电子邮件附件发送或生成 pdf 文件。 *`noframes`”格式会生成一个名为“`junit-noframes.html`”的文件,没有任何框架,并且不使用任何类型的重定向。 这种类型更适合作为电子邮件附件发送或生成 pdf 文件。
既然您已经了解了事物的内在运作方式,那么让我们进入有趣的部分(可能比拥有圣代冰淇淋更有趣)! 既然您已经了解了事物的内在运作方式,那么让我们进入有趣的部分(可能比拥有圣代冰淇淋更有趣)!
与那些无聊的自动生成的报告说再见,并欢迎**自定义**! 这意味着,我们将要编写或编辑某些内容。 那就是一个 XSL 文件,它位于下载的 apache-ant-1.10.2-bin.zip 的“etc”目录中。 让我们走简单的路线,就是编辑! 与那些无聊的自动生成的报告说再见,并欢迎**自定义**! 这意味着,我们将要编写或编辑某些内容。 那就是一个 XSL 文件,它位于下载的`apache-ant-1.10.2-bin.zip`的“`etc`”目录中。 让我们走简单的路线,就是编辑!
还记得吗,我们有两种格式? 因此,要自定义: 还记得吗,我们有两种格式? 因此,要自定义:
* 框架 – 编辑 junit-frames.xsl * 框架 – 编辑`junit-frames.xsl`
* noframes – 编辑 junit-noframes.xsl * noframes – 编辑`junit-noframes.xsl`
就我而言,这两个 XSL 文件都位于“E:\ apache-ant-1.10.2 \ etc”路径中。 作为本文的一部分,让我们研究“框架”格式。 这意味着我们将编辑“`junit-frames.xsl`”文件。 复制此文件并将其粘贴到项目路径中。 不要重命名文件。 为了使所有更改都能正常进行,请向报告任务添加属性`styledir`,如下所示。 这指定了定义的样式表 junit-frames.xsl 的位置。 `.`指定此样式表位于当前目录中(与构建文件的目录相同)。 就我而言,这两个 XSL 文件都位于“`E:\apache-ant-1.10.2\etc`”路径中。 作为本文的一部分,让我们研究“框架”格式。 这意味着我们将编辑“`junit-frames.xsl`”文件。 复制此文件并将其粘贴到项目路径中。 不要重命名文件。 为了使所有更改都能正常进行,请向报告任务添加属性`styledir`,如下所示。 这指定了定义的样式表`junit-frames.xsl`的位置。 `.`指定此样式表位于当前目录中(与构建文件的目录相同)。
`<report styledir="." format="frames" todir="junit/report"/>` `<report styledir="." format="frames" todir="junit/report"/>`
...@@ -69,22 +69,26 @@ Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告, ...@@ -69,22 +69,26 @@ Apache ANT 的 JunitReport 任务通过执行以下步骤来生成报告,
* 在标题部分添加徽标 * 在标题部分添加徽标
* 修改静态文字 * 修改静态文字
**注意**:因此,所有更改都是对放置在项目目录“E:\ Selenium \”(其中 Selenium 中的“junit-frames.xsl”)进行的 在我的情况下是项目名称)。* **注意**:因此,所有更改都是对放置在项目目录“`E:\Selenium\`”(其中 Selenium 中的“`junit-frames.xsl`”)进行的 在我的情况下是项目名称)。*
## 更改报告标题和描述 ## 更改报告标题和描述
在项目位置的“junit-frames.xsl”样式表中, 在项目位置的“`junit-frames.xsl`”样式表中,
1.将参数名称的文本(标题为 TITLE)更改为“JUnit Report”。 1.将参数名称的文本(标题为`TITLE`)更改为“`JUnit Report`”。
#### 之前的 #### 之前的
`<xsl:param name="TITLE">Unit Test Results.</xsl:param>` ```xml
<xsl:param name="TITLE">Unit Test Results.</xsl:param>
```
#### 之后 #### 之后
`<!-- <xsl:param name="TITLE">Unit Test Results.</xsl:param> --> ```xml
<xsl:param name="TITLE">JUnit Report</xsl:param>` <!-- <xsl:param name="TITLE">Unit Test Results.</xsl:param> -->
<xsl:param name="TITLE">JUnit Report</xsl:param>
```
2.“页眉”部分负责显示标题和右侧显示的说明。 将它们分别更改为“自定义 JUnit 报告”和“由忍者设计!”。 2.“页眉”部分负责显示标题和右侧显示的说明。 将它们分别更改为“自定义 JUnit 报告”和“由忍者设计!”。
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
> 原文: [https://javabeginnerstutorial.com/selenium/10e-advanced-webdriver-generating-pdf-report/](https://javabeginnerstutorial.com/selenium/10e-advanced-webdriver-generating-pdf-report/) > 原文: [https://javabeginnerstutorial.com/selenium/10e-advanced-webdriver-generating-pdf-report/](https://javabeginnerstutorial.com/selenium/10e-advanced-webdriver-generating-pdf-report/)
嗨冠军! 到目前为止,我们在报告方面已经涵盖了很多基础,您已经达到了高潮文章。 如果要将 HTML 报告附加到电子邮件并将其发送给利益相关者,则生成 HTML 报告可能没有帮助。 由于 ANT 生成的 JUnit 报告具有 index.html 文件,该文件又嵌入了其他一些 HTML 文件,例如 overview-frame.html,所有类-frame.html 和 overview-summary.html 文件。 嗨冠军! 到目前为止,我们在报告方面已经涵盖了很多基础,您已经达到了高潮文章。 如果要将 HTML 报告附加到电子邮件并将其发送给利益相关者,则生成 HTML 报告可能没有帮助。 由于 ANT 生成的 JUnit 报告具有`index.html`文件,该文件又嵌入了其他一些 HTML 文件,例如`overview-frame.html``allclass-frame.html``overview-summary.html`文件。
在这种情况下我们该怎么办? 我们如何将其附加到电子邮件? 我们不能将其作为单个文件而不是一组 HTML 文件获得吗? – 所有这些问题的唯一答案是,生成一个 **PDF 文件** 在这种情况下我们该怎么办? 我们如何将其附加到电子邮件? 我们不能将其作为单个文件而不是一组 HTML 文件获得吗? – 所有这些问题的唯一答案是,生成一个 **PDF 文件**
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
## 步骤 1: ## 步骤 1:
我们需要 JUnit PDF 报告*要点*分发。 它还包含所有必需的依赖项。 从链接“https://sourceforge.net/projects/junitpdfreport/files/”下载最新版本的“essentials” zip 文件。 我们需要 JUnit PDF 报告*要点*分发。 它还包含所有必需的依赖项。 从链接“https://sourceforge.net/projects/junitpdfreport/files/”下载最新版本的“必要” zip 文件。
![PDF Essentials Download](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20792%20382'%3E%3C/svg%3E)
<noscript><img alt="PDF Essentials Download" class="alignnone size-full wp-image-13353" height="382" sizes="(max-width: 792px) 100vw, 792px" src="img/f3daaf777964f1e44dde697809da3616.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload.jpg 792w, https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload-300x145.jpg 300w, https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload-768x370.jpg 768w" width="792"/><p>将文件内容解压缩到本地文件夹并记下路径。 另外,请确保分发中包含“build-junitpdfreport.xml”文件和“lib”文件夹以及某些其他文件和文件夹。</p><h2><strong>步骤 2:</strong></h2><p>是时候在 Eclipse IDE 中浏览“Build.xml”文件了。 有几行要添加。 首先要做的是告诉我们的项目,该 junitpdfreport 基本 zip 文件的内容在哪里提取。 在构建文件的&lt;项目&gt;标签中,将以下行及其路径添加到分发位置。</p><pre><code class="language-xml">&lt;!-- JUnit PDF report installation location --&gt; &lt;import file="E:/junitpdfreport_essentials_1_0/build-junitpdfreport.xml"/&gt;</code></pre><h2><strong>步骤 3:</strong></h2><p>将以下目标也添加到构建文件中,以便 ANT 知道该怎么做。</p><pre><code class="language-xml">&lt;!-- PDF Report --&gt; &lt;target name="pdfReport"&gt; &lt;junitpdfreport todir="${junit.output.dir}" styledir="default"&gt; &lt;fileset dir="${junit.output.dir}"&gt; &lt;include name="TEST-*.xml"/&gt; &lt;/fileset&gt; &lt;/junitpdfreport&gt; &lt;/target&gt;</code></pre><p>让我们尝试一次解密这一行,</p><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span> 1\. <code data-enlighter-language="xml">junitpdfreport todir="${junit.output.dir}"</code></p><p>– 这是将生成的 pdf 报告保存的位置。</p><p>2\. <code data-enlighter-language="xml">fileset dir="${junit.output.dir}" </code></p><p>– 提供所有 JUnit 测试结果所在的位置(请记住为执行的每个测试生成的 XML 文件,格式为“TEST-*。xml”)。</p><p>就我而言,我有以下测试文件:TEST-com.blog.junitTests.RadioBtns_Checkboxes.xml,TEST-com.blog.junitTests.SelectItems.xml 和 TESTS-TestSuites.xml 保存在位置'junit / '。</p><h2><strong>步骤 4:</strong></h2><p>右键单击并选择“2 ANT Build”,然后在“编辑配置”弹出窗口中选中“目标”标签。 确保已选中“pdfReport”目标,它是执行顺序中提到的最后一个目标。</p><p><img alt="PDF target configuration" class="alignnone size-full wp-image-13358" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/24_pdfTargetConfig.jpg" height="521" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20782%20521'%3E%3C/svg%3E" width="782"/></p><noscript><img alt="PDF target configuration" class="alignnone size-full wp-image-13358" height="521" src="img/3afeff61fc34dbc54a4173a3a68f5898.png" width="782"/><p>点击“运行”以执行构建文件。</p><h2><strong>步骤 5:</strong></h2><p>在指定的输出目录(本例中为“junit /”)中验证生成的 PDF 报告。</p><p><img alt="JUnit Folder" class="alignnone size-full wp-image-13354" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/26_pdfJnitFolder.jpg" height="397" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20461%20397'%3E%3C/svg%3E" width="461"/></p><noscript><img alt="JUnit Folder" class="alignnone size-full wp-image-13354" height="397" src="img/aa7d07ec593be85f4cb61b59a93dfac6.png" width="461"/><p>生成的 PDF 文件共有 8 页。 例如,前 5 页如下所示,</p><p><img alt="PDF report part 1" class="alignnone size-full wp-image-13355" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/27_pdf_Report1.jpg" height="493" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20872%20493'%3E%3C/svg%3E" width="872"/></p><noscript><img alt="PDF report part 1" class="alignnone size-full wp-image-13355" height="493" src="img/355dee55d351b9aac326eded2118a8db.png" width="872"/><p><img alt="PDF Report part 2" class="alignnone size-full wp-image-13356" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/28_pdf_Report2-1.jpg" height="472" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20873%20472'%3E%3C/svg%3E" width="873"/></p><noscript><img alt="PDF Report part 2" class="alignnone size-full wp-image-13356" height="472" src="img/a9d33aeeff32831e2eb61b9318a47817.png" width="873"/><p>最后但并非最不重要的是,该构建文件的快照显示了 HTML 报告和 PDF 报告目标代码,</p><p><img alt="ANT buildfile" class="alignnone size-full wp-image-13357" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/29_pdfBuildFile-1.jpg" height="592" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20873%20592'%3E%3C/svg%3E" width="873"/></p><noscript><img alt="ANT buildfile" class="alignnone size-full wp-image-13357" height="592" src="img/42bef927c7f8c0a40ebd7edc15606835.png" width="873"/><p>在您达到本文结尾时,让我们利用练习的力量。 在另一个主题上再见,让您掌握 WebDriver!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/" title="10f. Advanced WebDriver – Taking a Screenshot"> 10f。 高级 WebDriver – 截屏</a></h5></div></div> </body> </html></noscript> <noscript><img alt="PDF Essentials Download" class="alignnone size-full wp-image-13353" height="382" sizes="(max-width: 792px) 100vw, 792px" src="img/f3daaf777964f1e44dde697809da3616.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload.jpg 792w, https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload-300x145.jpg 300w, https://javabeginnerstutorial.com/wp-content/uploads/2018/06/25_pdfEssentialsDownload-768x370.jpg 768w" width="792"/><p>将文件内容解压缩到本地文件夹并记下路径。 另外,请确保分发中包含“build-junitpdfreport.xml”文件和“lib”文件夹以及某些其他文件和文件夹。</p><h2><strong>步骤 2:</strong></h2><p>是时候在 Eclipse IDE 中浏览“Build.xml”文件了。 有几行要添加。 首先要做的是告诉我们的项目,该 junitpdfreport 基本 zip 文件的内容在哪里提取。 在构建文件的&lt;项目&gt;标签中,将以下行及其路径添加到分发位置。</p><pre><code class="language-xml">&lt;!-- JUnit PDF report installation location --&gt; &lt;import file="E:/junitpdfreport_essentials_1_0/build-junitpdfreport.xml"/&gt;</code></pre><h2><strong>步骤 3:</strong></h2><p>将以下目标也添加到构建文件中,以便 ANT 知道该怎么做。</p><pre><code class="language-xml">&lt;!-- PDF Report --&gt; &lt;target name="pdfReport"&gt; &lt;junitpdfreport todir="${junit.output.dir}" styledir="default"&gt; &lt;fileset dir="${junit.output.dir}"&gt; &lt;include name="TEST-*.xml"/&gt; &lt;/fileset&gt; &lt;/junitpdfreport&gt; &lt;/target&gt;</code></pre><p>让我们尝试一次解密这一行,</p><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span> 1\. <code data-enlighter-language="xml">junitpdfreport todir="${junit.output.dir}"</code></p><p>– 这是将生成的 pdf 报告保存的位置。</p><p>2\. <code data-enlighter-language="xml">fileset dir="${junit.output.dir}" </code></p><p>– 提供所有 JUnit 测试结果所在的位置(请记住为执行的每个测试生成的 XML 文件,格式为“TEST-*。xml”)。</p><p>就我而言,我有以下测试文件:TEST-com.blog.junitTests.RadioBtns_Checkboxes.xml,TEST-com.blog.junitTests.SelectItems.xml 和 TESTS-TestSuites.xml 保存在位置'junit / '。</p><h2><strong>步骤 4:</strong></h2><p>右键单击并选择“2 ANT Build”,然后在“编辑配置”弹出窗口中选中“目标”标签。 确保已选中“pdfReport”目标,它是执行顺序中提到的最后一个目标。</p><p><img alt="PDF target configuration" class="alignnone size-full wp-image-13358" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/24_pdfTargetConfig.jpg" height="521" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20782%20521'%3E%3C/svg%3E" width="782"/></p><noscript><img alt="PDF target configuration" class="alignnone size-full wp-image-13358" height="521" src="img/3afeff61fc34dbc54a4173a3a68f5898.png" width="782"/><p>点击“运行”以执行构建文件。</p><h2><strong>步骤 5:</strong></h2><p>在指定的输出目录(本例中为“junit /”)中验证生成的 PDF 报告。</p><p><img alt="JUnit Folder" class="alignnone size-full wp-image-13354" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/26_pdfJnitFolder.jpg" height="397" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20461%20397'%3E%3C/svg%3E" width="461"/></p><noscript><img alt="JUnit Folder" class="alignnone size-full wp-image-13354" height="397" src="img/aa7d07ec593be85f4cb61b59a93dfac6.png" width="461"/><p>生成的 PDF 文件共有 8 页。 例如,前 5 页如下所示,</p><p><img alt="PDF report part 1" class="alignnone size-full wp-image-13355" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/27_pdf_Report1.jpg" height="493" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20872%20493'%3E%3C/svg%3E" width="872"/></p><noscript><img alt="PDF report part 1" class="alignnone size-full wp-image-13355" height="493" src="img/355dee55d351b9aac326eded2118a8db.png" width="872"/><p><img alt="PDF Report part 2" class="alignnone size-full wp-image-13356" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/28_pdf_Report2-1.jpg" height="472" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20873%20472'%3E%3C/svg%3E" width="873"/></p><noscript><img alt="PDF Report part 2" class="alignnone size-full wp-image-13356" height="472" src="img/a9d33aeeff32831e2eb61b9318a47817.png" width="873"/><p>最后但并非最不重要的是,该构建文件的快照显示了 HTML 报告和 PDF 报告目标代码,</p><p><img alt="ANT buildfile" class="alignnone size-full wp-image-13357" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/06/29_pdfBuildFile-1.jpg" height="592" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20873%20592'%3E%3C/svg%3E" width="873"/></p><noscript><img alt="ANT buildfile" class="alignnone size-full wp-image-13357" height="592" src="img/42bef927c7f8c0a40ebd7edc15606835.png" width="873"/><p>在您达到本文结尾时,让我们利用练习的力量。 在另一个主题上再见,让您掌握 WebDriver!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/" title="10f. Advanced WebDriver – Taking a Screenshot"> 10f。 高级 WebDriver – 截屏</a></h5></div></div> </body> </html></noscript>
\ No newline at end of file
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
## 步骤 1: ## 步骤 1:
使用 Selenium WebDriver 提供的“TakesScreenshot”界面。 将 WebDriver 对象强制转换为 TakesScreenshot 类型。 使用 Selenium WebDriver 提供的`TakesScreenshot`接口。 将 WebDriver 对象强制转换为`TakesScreenshot`类型。
当您在 TakesScreenshot 下面看到一条弯曲的线时,只需单击`import org.openqa.selenium.TakesScreenshot;`包,您将没有任何错误。 当您在`TakesScreenshot`下面看到一条弯曲的线时,只需单击`import org.openqa.selenium.TakesScreenshot;`包,您将没有任何错误。
```java ```java
// Cast driver object to TakesScreenshot // Cast driver object to TakesScreenshot
...@@ -21,7 +21,7 @@ TakesScreenshot screenshot = (TakesScreenshot) driver; ...@@ -21,7 +21,7 @@ TakesScreenshot screenshot = (TakesScreenshot) driver;
## 第 2 步: ## 第 2 步:
要将屏幕截图获取为图像文件,请调用“getScreenshotAs”方法。 要将屏幕截图获取为图像文件,请调用“`getScreenshotAs`”方法。
波浪线? - 点击: 波浪线? - 点击:
...@@ -37,7 +37,7 @@ File src = screenshot.getScreenshotAs(OutputType.FILE); ...@@ -37,7 +37,7 @@ File src = screenshot.getScreenshotAs(OutputType.FILE);
## 步骤 3: ## 步骤 3:
将生成的图像文件复制到您选择的目标位置。 使用 FileUtils 类的 copyFile 方法可以轻松完成此操作。 重要的是要注意,此方法将引发 IOException。 因此,作为一种好习惯,请将这段代码包装在 try-catch 块中。 将生成的图像文件复制到您选择的目标位置。 使用`FileUtils`类的`copyFile`方法可以轻松完成此操作。 重要的是要注意,此方法将引发`IOException`。 因此,作为一种好习惯,请将这段代码包装在`try-catch`块中。
线条更弯曲? - 点击: 线条更弯曲? - 点击:
...@@ -48,9 +48,9 @@ import java.text.SimpleDateFormat; ...@@ -48,9 +48,9 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
``` ```
确保完全按照指定的方式导入包。 通常,您可能需要下载 org.apache.commons.io jar(在撰写本文时为[下载位置](https://jar-download.com/explore-java-source-code.php?a=commons-io&g=commons-io&v=2.5&downloadable=1))并将其添加到项目的构建路径中。 之前,我们已经多次看到此过程,因此,我不再重复(请参阅[本文第 3 步](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/))。 确保完全按照指定的方式导入包。 通常,您可能需要下载`org.apache.commons.io` jar(在撰写本文时为[下载位置](https://jar-download.com/explore-java-source-code.php?a=commons-io&g=commons-io&v=2.5&downloadable=1))并将其添加到项目的构建路径中。 之前,我们已经多次看到此过程,因此,我不再重复(请参阅[本文第 3 步](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/))。
另外,请注意,我们在代码中将图像另存为.jpg 文件。 也可以将其另存为.png 文件。 另外,请注意,我们在代码中将图像另存为`.jpg`文件。 也可以将其另存为`.png`文件。
它可以很简单, 它可以很简单,
...@@ -81,7 +81,7 @@ public static String timestamp() { ...@@ -81,7 +81,7 @@ public static String timestamp() {
## 总体图片 ## 总体图片
使用两种方法创建一个名为“SaveScreenshot.java”的新类。 使用两种方法创建一个名为“`SaveScreenshot.java`”的新类。
1. `public static void capture(String testCaseName, WebDriver driver)` – 具有捕获屏幕快照并将其保存到所需位置的所有代码。 1. `public static void capture(String testCaseName, WebDriver driver)` – 具有捕获屏幕快照并将其保存到所需位置的所有代码。
2. `public static String timestamp()` – 用于生成时间戳并将其提供给上述方法,以使每个保存的屏幕截图都是唯一的。 2. `public static String timestamp()` – 用于生成时间戳并将其提供给上述方法,以使每个保存的屏幕截图都是唯一的。
...@@ -91,14 +91,14 @@ public static String timestamp() { ...@@ -91,14 +91,14 @@ public static String timestamp() {
1. 打开 Firefox 浏览器。 1. 打开 Firefox 浏览器。
2. 导航到 Google 帐户创建页面 2. 导航到 Google 帐户创建页面
3. 通过 ID 找到名字文本框 3. 通过 ID 找到名字文本框
4. 输入“fname01”作为名字 4. 输入“`fname01`”作为名字
5. 按名称找到姓氏文本框 5. 按名称找到姓氏文本框
6. 输入“lname01”作为姓氏 6. 输入“`lname01`”作为姓氏
7. 截取页面截图并将其保存到某个位置。 7. 截取页面截图并将其保存到某个位置。
## JUnit 代码: ## JUnit 代码:
1. ### SaveScreenshot.java 1. ### `SaveScreenshot.java`
```java ```java
package com.blog.junitTests; package com.blog.junitTests;
...@@ -138,7 +138,7 @@ public class SaveScreenshot { ...@@ -138,7 +138,7 @@ public class SaveScreenshot {
} }
``` ```
### 2\. Screenshot.java 类(执行示例方案部分中详细介绍的步骤) ### 2\. `Screenshot.java`类(执行示例方案部分中详细介绍的步骤)
```java ```java
package com.blog.junitTests; package com.blog.junitTests;
......
...@@ -120,10 +120,10 @@ public class SaveDocument { ...@@ -120,10 +120,10 @@ public class SaveDocument {
对“`SaveScreenshot.java`”文件进行了一些修改。 对“`SaveScreenshot.java`”文件进行了一些修改。
对我们在[先前文章](https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/)中创建的“SaveScreenshot.java”类进行了一些更改。 对我们在[先前文章](https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/)中创建的“`SaveScreenshot.java`”类进行了一些更改。
1. 删除生成时间戳的函数,并 1. 删除生成时间戳的函数,并
2. 文件扩展名从“.jpg”更改为“.png 2. 文件扩展名从“`.jpg`”更改为“`.png`
现在的代码看起来像这样, 现在的代码看起来像这样,
...@@ -160,16 +160,16 @@ public class SaveScreenshot { ...@@ -160,16 +160,16 @@ public class SaveScreenshot {
1. 打开 Firefox 浏览器。 1. 打开 Firefox 浏览器。
2. 导航到 Google 帐户创建页面 2. 导航到 Google 帐户创建页面
3. 通过 ID 找到名字文本框 3. 通过 ID 找到名字文本框
4. 输入“fname01”作为名字 4. 输入“`fname01`”作为名字
5. 截取屏幕截图,并将其命名为“testCaseName” +“1 5. 截取屏幕截图,并将其命名为“`testCaseName+1`
6. 按名称找到姓氏文本框 6. 按名称找到姓氏文本框
7. 输入“lname01”作为姓氏 7. 输入“`lname01`”作为姓氏
8. 截取屏幕截图并将其命名为“testCaseName” +“2 8. 截取屏幕截图并将其命名为“`testCaseName+2`
9. 在指定位置创建一个 word 文档,并将这两个屏幕截图都插入其中。 9. 在指定位置创建一个 word 文档,并将这两个屏幕截图都插入其中。
### JUnit 代码: ### JUnit 代码:
WordDocWithScreenshotTest.java `WordDocWithScreenshotTest.java`
```java ```java
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
我们将研究两个课程。 我们将研究两个课程。
1. SendMail.java – 此类包含用于发送电子邮件的所有代码。 1. `SendMail.java` – 此类包含用于发送电子邮件的所有代码。
2. InvokeMail.java – 通过提供从地址到地址,主题行和一些文本来调用 SendMail.java 2. `InvokeMail.java` – 通过提供从地址到地址,主题行和一些文本来调用`SendMail.java`
## 代码 ## 代码
...@@ -143,7 +143,7 @@ public class InvokeMail { ...@@ -143,7 +143,7 @@ public class InvokeMail {
将这些 JAR 添加到我们的项目构建路径中。 我们之前已经多次看到此程序,因此我不再重复(有关详细说明,请参阅此[文章](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/)的步骤 3)。 将这些 JAR 添加到我们的项目构建路径中。 我们之前已经多次看到此程序,因此我不再重复(有关详细说明,请参阅此[文章](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/)的步骤 3)。
### 了解 SendMail.java ### 了解`SendMail.java`
1.编写所有代码的方法,以便我们可以从任何类轻松地调用它。 1.编写所有代码的方法,以便我们可以从任何类轻松地调用它。
...@@ -228,9 +228,9 @@ message.setContent(multipart); ...@@ -228,9 +228,9 @@ message.setContent(multipart);
Transport.send(message); Transport.send(message);
``` ```
### 了解 InvokeMail.java ### 了解`InvokeMail.java`
此类很容易理解,因为我们只是通过提供所有必需的参数从 SendMail.java 调用“send”方法。 此类很容易理解,因为我们只是通过提供所有必需的参数从`SendMail.java`调用“`send`”方法。
```java ```java
String to[] = {"[[email protected]](/cdn-cgi/l/email-protection)"}; String to[] = {"[[email protected]](/cdn-cgi/l/email-protection)"};
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
右键单击项目->新建->包。 确保“源文件夹”显示您的项目名称,并将包名称设为“资源”。 右键单击项目->新建->包。 确保“源文件夹”显示您的项目名称,并将包名称设为“资源”。
现在,右键单击“资源”包->新建->文件。 让文件名是“config.properties”。 现在,右键单击“资源包->新建->文件”。 让文件名是“`config.properties`”。
![Folder Structure](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20350%20281'%3E%3C/svg%3E)
<noscript><img alt="Folder Structure" class="alignnone size-full wp-image-13555" height="281" sizes="(max-width: 350px) 100vw, 350px" src="img/b6caa2e423b83a552625a195bf5ccbf3.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/1_FolderStructue.jpg 350w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/1_FolderStructue-300x241.jpg 300w" width="350"/><h2><strong>步骤 2:</strong></h2><p>现在是时候从测试用例中提取所有这些硬编码的值了。</p><p>在“<strong> config.properties </strong>”文件中,将所有必需的属性作为键值对。 这将帮助我们在测试用例中引用每个属性及其密钥,该属性将在一分钟内演示。 可以在这个特定文件中对值进行任何更改,并且这些更改将神奇地反映在进行引用的所有测试用例中。</p><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span></p><p><img alt="property file" class="alignnone size-full wp-image-13576" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/3_PropertiesFile-1-1.jpg" height="241" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20737%20241'%3E%3C/svg%3E" width="737"/></p><noscript><img alt="property file" class="alignnone size-full wp-image-13576" height="241" src="img/30192b906e750b66936d351b54584625.png" width="737"/><h2><strong>步骤 3:</strong></h2><p>为了在测试案例中使用这些属性,</p><p><code data-enlighter-language="java">Properties props = new Properties(); </code></p><p>– 声明“属性”类型的“属性”变量。 这将创建一个没有默认值的空属性列表。</p><p>这需要从 java.util 包<code data-enlighter-language="java">import java.util.Properties;</code>导入</p><p><code data-enlighter-language="java">FileInputStream fis = new FileInputStream("resources//config.properties"); </code> –</p><p>创建一个连接以从“resources”包下的“config.properties”文件中读取所有属性。</p><p>这也需要从 java.io 包<code data-enlighter-language="java">import java.io.FileInputStream;</code>中导入</p><p><code data-enlighter-language="null">props.load(fis); </code>-使用打开的连接“fis”从输入字节流中读取所有属性。</p><p><code data-enlighter-language="java">props.getProperty("baseURL"); </code> – 要获取特定属性的值,请使用“getProperty”方法。 将在双引号中提及相应的键作为 getProperty()方法的参数。 它使用属性列表中的指定键搜索属性。 如果找不到密钥,则返回 null。</p><h2><strong>总体图片</strong></h2><p>让我们看一个测试案例,实现到目前为止所涵盖的概念,</p><h3><strong> <em>场景</em> </strong></h3><ol><li>打开 Firefox 浏览器。</li><li>从属性文件中读取 firefox 驱动程序路径和基本 URL。</li><li>导航到<a href="https://chandanachaitanya.github.io/selenium-practice-site/">演示站点</a></li><li>按名称找到“三轮车”复选框,然后将相应的消息打印到控制台</li><li>检查“三轮车”复选框是否已启用,并将相应消息打印到控制台</li><li>根据“货车”和“SUV”复选框的当前选择状态,选中或取消选中并打印执行 click()动作前后的状态</li><li>使用 XPath 找到“轿车”复选框</li><li>使用两次迭代在选择状态和取消选择状态之间切换</li><li>使用 cssSelector 找到“杂志”单选按钮</li><li>检查是否默认选中</li><li>如果是,则将相应消息打印到控制台,如果否,请选择单选按钮</li></ol><p>验证 Eclipse IDE 控制台输出屏幕和 JUnit 窗格是否成功</p><h3><strong>此方案的 JUnit 代码为</strong></h3><p><strong> Config.properties </strong></p><pre><code class="language-java">#Properties as key-value pairs baseUrl=https://chandanachaitanya.github.io/selenium-practice-site/ logoPath=E:\\Selenium\\Logo.png <a class="__cf_email__" data-cfemail="62050f030b0e5f160711160710525322050f030b0e4c010d0f" href="/cdn-cgi/l/email-protection">[email protected]</a> pdfReportPath=E:\\Selenium\\junit.pdf firefoxPath=E:\\Softwares\\Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe chromePath=browser-drivers\\chromedriver.exe IEPath=browser-drivers\\IEDriverServer.exe</code></pre><p><strong> RadioBtns_Checkboxes.java </strong></p><pre><code class="language-java">package com.blog.junitTests; import java.io.FileInputStream; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class RadioBtns_Checkboxes { // Declaring variables private WebDriver driver; private String baseUrl; Properties props; @Before public void setUp() throws Exception { // Creates an empty property list props = new Properties(); // A connection is created to config.properties file FileInputStream fis = new FileInputStream("resources//config.properties"); // Reads the properties from the input byte stream props.load(fis); // Get the firefox driver path from property file String firefoxPath = props.getProperty("firefoxPath"); // Assign the URL to be invoked to a String variable baseUrl = props.getProperty("baseUrl"); // Mention the property where required System.setProperty("webdriver.gecko.driver", firefoxPath); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate 'Tricycle' checkbox using name WebElement tricycleCheckbox = driver.findElement(By.name("vehicle2")); // Check if tricyle is displayed System.out.println("Is tricycle displayed? "+ tricycleCheckbox.isDisplayed()); // Check if tricyle is enabled to select if (tricycleCheckbox.isEnabled()) { // Click if enabled tricycleCheckbox.click(); } else { // Print message to console if disabled System.out.println("Unable to select 'Tricycle' checkbox as it is disabled."); } //Get all checkbox elements in a list List&lt;WebElement&gt; list = driver.findElements(By .cssSelector("input[type='checkbox']")); // Loops through all checkboxe elements for (int i = 0; i &lt; list.size(); i++) { // Checking if the checkbox is a 'Van' or 'SUV' if ((list.get(i).getAttribute("value").trim() .equalsIgnoreCase("van")) || (list.get(i).getAttribute("value").trim() .equalsIgnoreCase("suv"))) { // Print selection status to console System.out.println("BEFORE: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); // Check if the checkbox is selected if (!(list.get(i).isSelected())) { // Click the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } else { // Uncheck the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } System.out.println("Next..."); } } // Locate 'Sedan' checkbox using xPath WebElement sedanCheckbox = driver.findElement(By .xpath("//input[@name='vehicle5']")); System.out.println("Trying to select and de-select Sedan checkbox..."); for (int i = 0; i &lt; 2; i++) { // Click the checkbox sedanCheckbox.click(); // Print current status to console System.out.println("Selection status of 'Sedan' checkbox: " + sedanCheckbox.isSelected()); } // Locate 'Magazines' radio button using cssSelector WebElement magazinesRadioBtn = driver.findElement(By .cssSelector("input[value='Magazines']")); // Check if radio button is selected by default if (magazinesRadioBtn.isSelected()) { // Print message to console System.out.println("Magazines radio button is selected by default"); } else { // Click the radio button magazinesRadioBtn.click(); } } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } }</code></pre><h2><strong>输出</strong></h2><p>每行代码都提供了清晰的注释,使其易于说明。 执行测试用例后,eclipse IDE 控制台窗口的输出如下,</p><p><img alt="property file eclipse output" class="alignnone size-full wp-image-13553" data-lazy-sizes="(max-width: 678px) 100vw, 678px" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg" data-lazy-srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg 678w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput-300x209.jpg 300w" height="473" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20678%20473'%3E%3C/svg%3E" width="678"/></p><noscript><img alt="property file eclipse output" class="alignnone size-full wp-image-13553" height="473" sizes="(max-width: 678px) 100vw, 678px" src="img/259ea2204c75dfa434e16dd2865ada9a.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg 678w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput-300x209.jpg 300w" width="678"/><p>属性文件使自动化测试人员的生活成真,梦想成真。 由此证明! 现在该尝试使用今天的概念了。 请务必戴好安全帽,以免碰到<a href="https://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-catch-java/">例外</a>! 还有一件事,我可以在<a href="https://github.com/JBTAdmin/Selenium/tree/master/AdvancedWebDriver/Property%20Files"> GitHub 存储库</a>中找到所有代码文件。 去看一下!</p><p>再见! 祝你有美好的一天!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10j-advanced-webdriver-reading-data-from-excel-using-poi/" title="10j. Advanced WebDriver – Reading data from excel using POI"> 10j。 先进的 WebDriver – 使用 POI 从 excel 读取数据</a></h5></div></div> </body> </html></noscript> <noscript><img alt="Folder Structure" class="alignnone size-full wp-image-13555" height="281" sizes="(max-width: 350px) 100vw, 350px" src="img/b6caa2e423b83a552625a195bf5ccbf3.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/1_FolderStructue.jpg 350w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/1_FolderStructue-300x241.jpg 300w" width="350"/><h2><strong>步骤 2:</strong></h2><p>现在是时候从测试用例中提取所有这些硬编码的值了。</p><p>在“<strong> config.properties </strong>”文件中,将所有必需的属性作为键值对。 这将帮助我们在测试用例中引用每个属性及其密钥,该属性将在一分钟内演示。 可以在这个特定文件中对值进行任何更改,并且这些更改将神奇地反映在进行引用的所有测试用例中。</p><p><span class="ezoic-adpicker-ad" id="ezoic-pub-ad-placeholder-124"> </span> <span class="ezoic-ad box-4 adtester-container adtester-container-124" data-ez-name="javabeginnerstutorial_com-box-4" style="display:block !important;float:none;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:110px;min-width:728px;text-align:center !important;"> <span class="ezoic-ad" ezah="90" ezaw="728" id="div-gpt-ad-javabeginnerstutorial_com-box-4-0" style="position:relative;z-index:0;display:inline-block;min-height:90px;min-width:728px;"> </span> </span></p><p><img alt="property file" class="alignnone size-full wp-image-13576" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/3_PropertiesFile-1-1.jpg" height="241" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20737%20241'%3E%3C/svg%3E" width="737"/></p><noscript><img alt="property file" class="alignnone size-full wp-image-13576" height="241" src="img/30192b906e750b66936d351b54584625.png" width="737"/><h2><strong>步骤 3:</strong></h2><p>为了在测试案例中使用这些属性,</p><p><code data-enlighter-language="java">Properties props = new Properties(); </code></p><p>– 声明“属性”类型的“属性”变量。 这将创建一个没有默认值的空属性列表。</p><p>这需要从 java.util 包<code data-enlighter-language="java">import java.util.Properties;</code>导入</p><p><code data-enlighter-language="java">FileInputStream fis = new FileInputStream("resources//config.properties"); </code> –</p><p>创建一个连接以从“resources”包下的“config.properties”文件中读取所有属性。</p><p>这也需要从 java.io 包<code data-enlighter-language="java">import java.io.FileInputStream;</code>中导入</p><p><code data-enlighter-language="null">props.load(fis); </code>-使用打开的连接“fis”从输入字节流中读取所有属性。</p><p><code data-enlighter-language="java">props.getProperty("baseURL"); </code> – 要获取特定属性的值,请使用“getProperty”方法。 将在双引号中提及相应的键作为 getProperty()方法的参数。 它使用属性列表中的指定键搜索属性。 如果找不到密钥,则返回 null。</p><h2><strong>总体图片</strong></h2><p>让我们看一个测试案例,实现到目前为止所涵盖的概念,</p><h3><strong> <em>场景</em> </strong></h3><ol><li>打开 Firefox 浏览器。</li><li>从属性文件中读取 firefox 驱动程序路径和基本 URL。</li><li>导航到<a href="https://chandanachaitanya.github.io/selenium-practice-site/">演示站点</a></li><li>按名称找到“三轮车”复选框,然后将相应的消息打印到控制台</li><li>检查“三轮车”复选框是否已启用,并将相应消息打印到控制台</li><li>根据“货车”和“SUV”复选框的当前选择状态,选中或取消选中并打印执行 click()动作前后的状态</li><li>使用 XPath 找到“轿车”复选框</li><li>使用两次迭代在选择状态和取消选择状态之间切换</li><li>使用 cssSelector 找到“杂志”单选按钮</li><li>检查是否默认选中</li><li>如果是,则将相应消息打印到控制台,如果否,请选择单选按钮</li></ol><p>验证 Eclipse IDE 控制台输出屏幕和 JUnit 窗格是否成功</p><h3><strong>此方案的 JUnit 代码为</strong></h3><p><strong> Config.properties </strong></p><pre><code class="language-java">#Properties as key-value pairs baseUrl=https://chandanachaitanya.github.io/selenium-practice-site/ logoPath=E:\\Selenium\\Logo.png <a class="__cf_email__" data-cfemail="62050f030b0e5f160711160710525322050f030b0e4c010d0f" href="/cdn-cgi/l/email-protection">[email protected]</a> pdfReportPath=E:\\Selenium\\junit.pdf firefoxPath=E:\\Softwares\\Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe chromePath=browser-drivers\\chromedriver.exe IEPath=browser-drivers\\IEDriverServer.exe</code></pre><p><strong> RadioBtns_Checkboxes.java </strong></p><pre><code class="language-java">package com.blog.junitTests; import java.io.FileInputStream; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class RadioBtns_Checkboxes { // Declaring variables private WebDriver driver; private String baseUrl; Properties props; @Before public void setUp() throws Exception { // Creates an empty property list props = new Properties(); // A connection is created to config.properties file FileInputStream fis = new FileInputStream("resources//config.properties"); // Reads the properties from the input byte stream props.load(fis); // Get the firefox driver path from property file String firefoxPath = props.getProperty("firefoxPath"); // Assign the URL to be invoked to a String variable baseUrl = props.getProperty("baseUrl"); // Mention the property where required System.setProperty("webdriver.gecko.driver", firefoxPath); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate 'Tricycle' checkbox using name WebElement tricycleCheckbox = driver.findElement(By.name("vehicle2")); // Check if tricyle is displayed System.out.println("Is tricycle displayed? "+ tricycleCheckbox.isDisplayed()); // Check if tricyle is enabled to select if (tricycleCheckbox.isEnabled()) { // Click if enabled tricycleCheckbox.click(); } else { // Print message to console if disabled System.out.println("Unable to select 'Tricycle' checkbox as it is disabled."); } //Get all checkbox elements in a list List&lt;WebElement&gt; list = driver.findElements(By .cssSelector("input[type='checkbox']")); // Loops through all checkboxe elements for (int i = 0; i &lt; list.size(); i++) { // Checking if the checkbox is a 'Van' or 'SUV' if ((list.get(i).getAttribute("value").trim() .equalsIgnoreCase("van")) || (list.get(i).getAttribute("value").trim() .equalsIgnoreCase("suv"))) { // Print selection status to console System.out.println("BEFORE: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); // Check if the checkbox is selected if (!(list.get(i).isSelected())) { // Click the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } else { // Uncheck the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } System.out.println("Next..."); } } // Locate 'Sedan' checkbox using xPath WebElement sedanCheckbox = driver.findElement(By .xpath("//input[@name='vehicle5']")); System.out.println("Trying to select and de-select Sedan checkbox..."); for (int i = 0; i &lt; 2; i++) { // Click the checkbox sedanCheckbox.click(); // Print current status to console System.out.println("Selection status of 'Sedan' checkbox: " + sedanCheckbox.isSelected()); } // Locate 'Magazines' radio button using cssSelector WebElement magazinesRadioBtn = driver.findElement(By .cssSelector("input[value='Magazines']")); // Check if radio button is selected by default if (magazinesRadioBtn.isSelected()) { // Print message to console System.out.println("Magazines radio button is selected by default"); } else { // Click the radio button magazinesRadioBtn.click(); } } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } }</code></pre><h2><strong>输出</strong></h2><p>每行代码都提供了清晰的注释,使其易于说明。 执行测试用例后,eclipse IDE 控制台窗口的输出如下,</p><p><img alt="property file eclipse output" class="alignnone size-full wp-image-13553" data-lazy-sizes="(max-width: 678px) 100vw, 678px" data-lazy-src="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg" data-lazy-srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg 678w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput-300x209.jpg 300w" height="473" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20678%20473'%3E%3C/svg%3E" width="678"/></p><noscript><img alt="property file eclipse output" class="alignnone size-full wp-image-13553" height="473" sizes="(max-width: 678px) 100vw, 678px" src="img/259ea2204c75dfa434e16dd2865ada9a.png" srcset="https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput.jpg 678w, https://javabeginnerstutorial.com/wp-content/uploads/2018/07/2_EclipseOutput-300x209.jpg 300w" width="678"/><p>属性文件使自动化测试人员的生活成真,梦想成真。 由此证明! 现在该尝试使用今天的概念了。 请务必戴好安全帽,以免碰到<a href="https://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-catch-java/">例外</a>! 还有一件事,我可以在<a href="https://github.com/JBTAdmin/Selenium/tree/master/AdvancedWebDriver/Property%20Files"> GitHub 存储库</a>中找到所有代码文件。 去看一下!</p><p>再见! 祝你有美好的一天!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/10j-advanced-webdriver-reading-data-from-excel-using-poi/" title="10j. Advanced WebDriver – Reading data from excel using POI"> 10j。 先进的 WebDriver – 使用 POI 从 excel 读取数据</a></h5></div></div> </body> </html></noscript>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册