简体   繁体   English

使用Selenium测试Javascript

[英]Testing Javascript Using Selenium

I am trying to test a website. 我正在尝试测试一个网站。 I am able to access most web elements, except the menu items. 除了菜单项,我能够访问大多数Web元素。

Basically there is a drop-down menu, I managed to click on the menu using Selenium, thus opening the drop down menu. 基本上有一个下拉菜单,我设法使用Selenium单击该菜单,从而打开了下拉菜单。 Now I need to click on an element in the menu, but the menu seems to be written in Javascript, not as web elements, so I cannot find a way to access the menu items. 现在,我需要单击菜单中的一个元素,但是菜单似乎是用Javascript编写的,而不是作为Web元素编写的,因此我找不到找到访问菜单项的方法。

Any Ideas? 有任何想法吗? I specifically need to do this in Selenium. 我特别需要在Selenium中执行此操作。

UPDATE: -------------------------------------------------------------------------------------- The menu on the website might be a context menu. 更新:------------------------------------------------ --------------------------------------网站上的菜单可能是上下文菜单。 It is opened using the left click. 使用鼠标左键打开。 Any ideas how I can access its options? 有什么想法可以访问其选项吗?

The menu may be constructed and manipulated with the page's javascript, but it will exist as html. 菜单可以使用页面的javascript进行构造和操作,但是它将以html的形式存在。 You can interact with this html using Selenium. 您可以使用Selenium与该html进行交互。

 WebElement dropDownListBox = driver.findElement(By.id("Menu_id")); 
                  Select clickThis = new Select(dropDownListBox);                
                  clickThis.selectByVisibleText(activity); //If you know visible text
                //clickThis.getFirstSelectedOption();  // This will auto select first option in the drop down                
               // clickThis.selectByValue(1); //if you the value of drop down list items

Opening a contextmenu isn't that easy. 打开上下文菜单并不是那么容易。 Till now, I'm triggering an mouse-event via execution of a javascript (didn't find an other solution). 到目前为止,我正在通过执行javascript触发鼠标事件(找不到其他解决方案)。 It's simular to that discussion: Trigger right-click or Selenium 2 right-click 与该讨论类似: 右键单击触发Selenium 2右键单击

Sometimes it's useful, if you are are just triggering the underlying js-function. 如果您只是触发底层的js函数,有时它会很有用。 Most of the time the developer can give you one entrypoint to call. 在大多数情况下,开发人员可以给您一个入口点进行呼叫。 This will give you the possibility to keep on building your tests and after some time you implemented the javascript function which opens the contextmenu via triggering the event. 这将使您可以继续构建测试,并在一段时间后实现了javascript函数,该函数通过触发事件来打开上下文菜单。

I have not managed to access the menu items using Selenium other than by clicking the menu, and scrolling through the items using up and down arrows through selenium. 除了单击菜单并使用硒中的向上和向下箭头在菜单项中滚动外,我没有使用Selenium访问菜单项。

I used OpenScript in order to record/playback the test, and it has worked. 我使用OpenScript来记录/回放测试,并且它一直有效。 In my particular case, the website I was using required a flag to be set to make it testable. 在我的特定情况下,我使用的网站要求设置一个标志以使其可测试。 If you're encountering problems with testing a website, you might need to contact the developers and ask whether an "automation" flag has been set. 如果您在测试网站时遇到问题,则可能需要联系开发人员,并询问是否设置了“自动化”标志。

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

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