如何检查上下文菜单中的项目,右键单击后,Selenium?

时间:2014-03-18 09:33:44

标签: selenium

我正在编写一个测试(使用selenium)来验证在ai右键单击我们网站的某个特定部分后,它应该显示标准的上下文菜单(复制,过去,重新加载,另存储等)而不是我们的创建上下文菜单。

我需要找到一种方法来检查右键单击后的上下文菜单中的项目,任何想法?

我到目前为止的地方......

  private IWebDriver driver = null;
    private WebDriverWait wait = null;
    private Actions action;

    [TestInitialize()]
    public void Initialize()
    {
        driver = new ChromeDriver();
        driver.Navigate().GoToUrl("http://localhost/testwebportal");
        wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        action= new Actions(driver);
    }

    [TestMethod]

    public void Right_click_brochure_while_zoomed_in_ID_8_2()
    {
        // click brochure
        var clickFirstProduct =    driver.FindElement(By.CssSelector("div.MetaSearchBrochureTile:nth-child(1) > div:nth-child(1) > img:nth-child(2)"));
        clickFirstProduct.Click();
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1500));

        // zoom in
        var brochurePage = driver.FindElement(By.CssSelector(".p1"));
        brochurePage.Click();

        action.ContextClick(brochurePage);

        // code to check if context menu is not my created right click menu browser,               
        // by looking at the menu items after the right click.


        action.Perform();

    }

2 个答案:

答案 0 :(得分:0)

如果您正在寻找驱动硒的机器,请点击how to do a right click in seleniumhow to simulate a right click with code in selenium

答案 1 :(得分:0)

我希望这会有所帮助。如果我错过了解你的问题,请告诉我。既然你没有给我HTML结构,我做了很多假设。

ContextOption是一个字符串数组,包含上下文菜单中预期的选项。 我正在做的是比较预期的选项与上下文菜单中显示的选项。如果它们中的任何一个不匹配,低于您的预期或超出您的预期,则返回false

请记住用适当的元素定位器替换代码。

    var actions = new Actions(driver);

    WebElement webElement = driver.FindElement(By.XPath("")));
    actions.ContextClick(webElement).Perform();
    int numberOfOptionPresent = 0;
    foreach (var option in ContextOption)
    {
        IWebElement contextOptions = driver.FindElement(By.XPath(""));
        ReadOnlyCollection<IWebElement> totalContextOption = contextOptions.FindElements(By.TagName("li"));
        for (int c = 1; c <= totalContextOption.Count; c++)
        {
            string contextText = driver.FindElement(By.XPath("li[" + c + "]")).Text;

            if (contextText == option)
            {
                if (contextText != "")
                {
                    numberOfOptionPresent++;
                }
                break;
            }
            if (totalContextOption.Count == c)
            {
                 return false;
            }
        }
        if (numberOfOptionPresent == ContextOption.Count())
        {
            return true;
        }
        return false;
    }