简体   繁体   English

如何测试使用Selenium的Java代码?

[英]How to test my Java code that is using Selenium?

I have a Java application that is scanning a website using Selenium. 我有一个Java应用程序正在使用Selenium扫描网站。 It is crawling all the pages of the website. 它正在爬网网站的所有页面。 Some of these pages are generated dynamically by selecting a combination values, clicking some buttons. 通过选择组合值并单击一些按钮,可以动态生成其中的某些页面。 The purpose of this application is to crawl through all the pages of the website and save the HTML source and a screenshot of all the pages it comes across. 此应用程序的目的是爬网网站的所有页面,并保存HTML源代码和所遇到的所有页面的屏幕截图。 The content and structure of these webpages keep changing over time. 这些网页的内容和结构会随着时间不断变化。

The application is running fine, but the methods calling the webdriver and fetching the elements to enter a set combination of values and clicking buttons to get to all the pages need to be updated quite often as the HTML structure of the website is changing frequently. 该应用程序运行良好,但是由于网站的HTML结构经常变化,因此调用Webdriver并获取元素以输入值的组合的集合以及单击按钮以到达所有页面的方法需要经常更新。

Now my question is that: How can I test the functionality of my Java methods calling the Web Driver using Unit Tests How should I approach the unit tests to test the stability of my code that is finding HTML elements, filling in values, clicking buttons, getting the HTML source. 现在我的问题是:我如何使用单元测试来测试调用Web驱动程序的Java方法的功能,我该如何进行单元测试来测试代码的稳定性,该代码查找HTML元素,填充值,单击按钮,获取HTML源代码。 Currently I save a sample HTML file and test my code against it. 目前,我保存了一个示例HTML文件,并针对该文件测试我的代码。 But since I have to update my code and unit tests along with every HTML structure change, that defeats the purpose of unit tests, if I have to updated my unit tests as well (since the values on the page also change). 但是由于必须随同每次HTML结构更改一起更新代码和单元测试,因此如果我也必须更新单元测试(因为页面上的值也发生了更改),那将破坏单元测试的目的。

Please help me find an efficient or correct approach to testing my code using unit tests. 请帮助我找到一种有效或正确的方法来使用单元测试来测试我的代码。

I have been working on the same feature in this project Revolance UI Monitoring to do reviews on changes in the UI acrross several versions. 我一直在该项目Revolance UI Monitoring中使用相同的功能,以对UI的更改(包括多个版本)进行评论。

To answer your question, I solved the testing problem by using PhantomJS with a mocked website. 为了回答您的问题,我通过将PhantomJS与模拟网站一起使用解决了测试问题。 As you designed yourself the mocked website, you know what should be the expected outcome. 在为自己设计模拟网站时,您应该知道预期的结果。

I hope this help! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM