如何构建一个数据驱动的 Selenium WebDriver + Java + TestNG 框架

huangapple go评论80阅读模式
英文:

How to Build a Data Driven Selenium WebDriver + Java + TestNG Framework

问题

我计划使用Selenium WebDriver + Java + POM(页面对象模型)+ TestNG来自动化一些网站。

除了每个交易都会有一个几乎像表单的不同的Web页面外,我已经拥有了给定网站的所有其他通用Web页面。

所以,我有以下选择。

  1. 为所有静态通用页面创建一个页面对象模型(POM),然后开始为每个交易中不同的页面创建POM。
  2. 为所有静态通用页面创建一个页面对象模型(POM),并且使用一些外部数据源(XML、Excel等),我可以为该特定页面生成测试。

在这里,我更倾向于第二种方法,因为我不需要为每个新的交易都重新编写代码,仅因为有一个页面不同。

有什么想法吗?或者是否有人已经实现了类似的方法呢?

英文:

I'm planning to automate some website using Selenium WebDriver + Java + POM(Page Object Model) + TestNG.

I've all other WebPages common for the given website but for each transaction one WebPage which is almost like a form would be different.

So, I've following choices.

  1. Have a Page Object Model (POM) Created for all static common pages and Start creating POMs for the pages which differs in each transaction.
  2. Have a Page Object Model (POM) Created for all static common pages and using some external data source (XML, Excel etc) I can generate the tests for that perticular page.

I'm in favour of second approach here as I don't need to write code again for each new transaction just because there is one page is different?

Any thoughts? or anybody implemented something like this already?

答案1

得分: 1

是的,第二种方法——基于'Page Obj模型'的方法是保持代码隔离和易于维护的最佳选择。

对于您的测试数据维护,我建议您使用基于Cucumber的(行为驱动的)框架。

它非常适用于自动化FW(POM、Selenium、Java、TestNG/Junit和基于Maven)项目。

通过使用Cucumber,您无需依赖于任何其他的测试数据来源,比如Excel或XML,这可以很容易地通过Cucumber的特性文件进行维护。

此外,BDD使得'BA-QA-DEV和管理'保持在同一页面上的主要优势得以实现。
如果您不想使用Cucumber/BDD,那么您可以使用TestNG的数据提供者功能结合Excel来实现更好的测试数据管理。

如果您想学习Cucumber/BDD,那么在线上有很多非常好的视频教程可供学习。我最喜欢的一个在这里——
https://www.youtube.com/playlist?list=PL6tu16kXT9PpteusHGISu_lHcV6MbBtA6

关于Web阅读:
https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/

祝您测试愉快!

英文:

Yes 2nd approach- 'Page Obj model' based is best to keep your code isolated and easily maintainable.

For your test data maintenance I'd suggest you using Cucumber based (BDD driven) framework.

It goes very well with automation FW (POM, Selenium, Java, TestNG/Junit and Maven based) projects.

By using cucumber you need not depend on any other source of test data, i.e. excel or xml, this can easily be maintaned with feature files of cucumber.

Also BDD gives you the main advantage of keeping 'BA-QA-DEV and Management' on same page.
If you dont want to use Cucumber/BDD, then you can use Test NG data provider feature with Excel to achieve better test data management.

If you want to learn Cucumber/BDD-> there are lots of very good video tutorials available online. One of my Fav is here-
https://www.youtube.com/playlist?list=PL6tu16kXT9PpteusHGISu_lHcV6MbBtA6

for web reading:
https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/

Happy Testing!

答案2

得分: 0

我在类似的项目中已经工作过。我建议选择方案#1。原因是将来可能会发现网页上的差异,所以通用函数不一定适用于每个页面。

所以,如果您现在选择#2,那没问题,但在这种情况下最终会转向#1。

英文:

I have worked in similar kind of project. I would suggest to with #1. The reason is that might be possible in future you find difference in web pages, So common function will not be always applicable to each page.

So if you go with #2 as of now then its fine but you gonna end up by following #1 in such cases.

答案3

得分: 0

上面的回答将页面对象模型与数据驱动框架混合在一起。
基本上在数据驱动框架中,数据是从外部文件中读取的。

如果您想要构建一个简单的纯数据驱动框架,那么它应该具有以下特点:

  1. 独立的测试
  2. 所有测试都应该从JSON/XML/YAML/XLS等任何源读取数据
  3. 具有您的定位器和其他设置的属性文件
  4. 您还应该创建一个基类,其中包含可以在测试中重复使用的常见功能
  5. 您应该以这样的方式制作它,以便通过更改外部标志,轻松地在GRID上运行,并且测试应该在GRID中运行
  6. 应该进行适当的HTML报告,包括屏幕截图、错误和失败。

还可以观看这个视频
https://youtu.be/s-W8pw9GnWc

英文:

The above answers are mixing the Page object model with a data-driven framework.
Basically in data-driven framework, the data is read from an external file

Well if you want to build a simple pure data-driven framework then it should have

  1. Independent tests
  2. All tests should read data from JSON/XML/YAML/XLS... any source
  3. Properties file having your locators and other settings
  4. You should also create a base class which will have the common reusable functions which can be used in tests
  5. You should make it in such a way that running on grid is easy and by just chaning external flag.. tests should run in GRID
  6. Proper HTML reporting with screenshots, errors, failures should be done

Also watch this video
https://youtu.be/s-W8pw9GnWc

huangapple
  • 本文由 发表于 2020年8月24日 19:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63560570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定