如何使用 Selenium、Java 自动化图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23128743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automate graphs using Selenium, Java
提问by Kazman
I inject data into a web application and it generates a graph and a pie chart. I'd like to test the result on the graph is coherent with the given data through Selenium. Any ideas? Thanks, Best regards !!!
我将数据注入到一个 Web 应用程序中,它生成一个图形和一个饼图。我想通过 Selenium 测试图表上的结果与给定数据是否一致。有任何想法吗?谢谢,最好的问候!!!
回答by Michele Festini
Assuming that your graph has a Javascript Model (e.g. an array) you can assert the contents of such array by using the assertEval command.
假设您的图形有一个 Javascript 模型(例如一个数组),您可以使用 assertEval 命令来断言此类数组的内容。
回答by Tehsil
I am posting the most basic but fundamental example for your Pie chart problem, Here I am taking Yahoo's YUI based PIE Chart for my example. Here in each refresh all the sections are created dynamically so I am using contains in the id of the element . Here in the chart the controls are not simple HTML but these are svg (HTML5 controls) so on finding these we need to use //* in xpath.
我正在为您的饼图问题发布最基本但最基本的示例,这里我以雅虎基于 YUI 的饼图为例。在每次刷新时,所有部分都是动态创建的,因此我在元素的 id 中使用 contains 。图表中的控件不是简单的 HTML,而是 svg(HTML5 控件),因此找到这些控件后,我们需要在 xpath 中使用 //*。
My motive here is to find these dynamic sections in the PIE chart(Here in the current chart there are 5 sections)
我的动机是在 PIE 图表中找到这些动态部分(当前图表中有 5 个部分)
And to click each section and to print tool tip text of these.
并单击每个部分并打印这些工具提示文本。
The Out put will be like. Violette Part:day: Monday taxes: 2000
输出将像。Violette 部分:天:星期一税:2000
Grey Part:day: Friday taxes: 2000
灰色部分:天:星期五税:2000
Light Violette Part:day: Thursday taxes: 200
淡紫色部分:天:星期四税:200
Green Part:day: Wednesday taxes: 4000
绿色部分:天:星期三税:4000
Brown Part:day: Tuesday taxes: 50 0.61%
棕色部分:天:周二税:50 0.61%
Here is the Demo program for it,anywhere you can execute it... :)
这是它的演示程序,您可以在任何地方执行它... :)
package tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testCode {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://yuilibrary.com/yui/docs/charts/charts-pie.html");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
**//FIND DIFFERENT SECTIONS IN PIE CHART**
WebElement ViolettePart = driver.findElement(By.xpath("//* [contains(@id,'yui_3_13_0_1_')][contains(@class,'yui3-svgSvgPieSlice')][@fill='#66007f']"));
WebElement GreenPart = driver.findElement(By.xpath("//*[contains(@id,'yui_3_13_0_1_')][contains(@class,'yui3-svgSvgPieSlice')][@fill='#295454']"));
WebElement GreyPart = driver.findElement(By.xpath("//*[contains(@id,'yui_3_13_0_1_')][contains(@class,'yui3-svgSvgPieSlice')][@fill='#e8cdb7']"));
WebElement LightViolettePart = driver.findElement(By.xpath("//*[contains(@id,'yui_3_13_0_1_')][contains(@class,'yui3-svgSvgPieSlice')][@fill='#996ab2']"));
WebElement BrownPart = driver.findElement(By.xpath("//*[contains(@id,'yui_3_13_0_1_')][contains(@class,'yui3-svgSvgPieSlice')][@fill='#a86f41']"));
**//TOOLTIP OVER PIE CHART**
WebElement ToolTip = driver.findElement(By.xpath("//div[contains(@id,'_tooltip')]"));
**//CLICK EACH SECTION OF PIE CHART AND GET THE TEXT OVER IT**
ViolettePart.click();
System.out.println("Violette Part:"+ToolTip.getText());
GreyPart.click();
System.out.println("Grey Part:"+ToolTip.getText());
LightViolettePart.click();
System.out.println("Light Violete Part:"+ToolTip.getText());
GreenPart.click();
System.out.println("Green Part:"+ToolTip.getText());
BrownPart.click();
System.out.println("Brown Part:"+ToolTip.getText());
} }
回答by vins
Unless you are ok with just verifying metadata about the chart (captions, some JSON properties, etc) you will have to use image comparison. You define a baseline image that represents what the result should like like and your test compares against the reference.
除非您只验证有关图表的元数据(标题、某些 JSON 属性等),否则您将不得不使用图像比较。您定义一个基线图像来表示结果应该是什么样的,并将您的测试与参考进行比较。
Since it is not trivial to reliably test images you can use a library such as Ocular.
由于可靠地测试图像并非易事,因此您可以使用诸如Ocular 之类的库。