Home > Blockchain >  Multiple Interaction in a single test script result into summing up the actions
Multiple Interaction in a single test script result into summing up the actions

Time:10-23

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Basics._03_Basic_Function
{
    class Slider
    {
        [Test]
        public void Test_Slider()
        {
            ChromeOptions options = new ChromeOptions();
            IWebDriver driver = new ChromeDriver(options);

            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);

            driver.Navigate().GoToUrl("https://jqueryui.com/slider/");

            IWebElement slderFrame = driver.FindElement(By.XPath("//iframe[@class='demo-frame']"));

            driver.SwitchTo().Frame(slderFrame);

            Thread.Sleep(2500);

            Actions action = new Actions(driver);
            
            // Offset 1 to Move Slider by 30 
            IWebElement slider = driver.FindElement(By.XPath("//*[@id='slider']/span[1]"));
            action.DragAndDropToOffset(slider, 30, 0).Build().Perform();
            Thread.Sleep(2500);

            // Offset 2 to Move Slider by 30
            action.DragAndDropToOffset(slider, 30, 0).Build().Perform();
            Thread.Sleep(2500);

            // Offset 3 to Move Slider by 30
            action.DragAndDropToOffset(slider, 30, 0).Build().Perform();
            Thread.Sleep(2500);

            driver.Close();
            driver.Quit();
        }
    }
}

Problem : There are three interactions in the above test script

Offset 1 -> *Moves the slider once by 30 *

Offset 2 -> Moves the slider twice by 30 each (total move by 60)

Offset 3 -> Moves the slider thrice by 30 each (total move by 90)

why these actions are summing up ?? How can it be protected to summing up ??

CodePudding user response:

Below code working fine. Release the action after each perform

using NUnit.Framework;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Basics._03_Basic_Function
{
    [Category("03 Basic Function")]
    class SliderTest
    {
        [Test]
        public void Test_Slider()
        {
            ChromeOptions options = new ChromeOptions();
            IWebDriver driver = new ChromeDriver(options);

            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);

            driver.Navigate().GoToUrl("https://jqueryui.com/slider/");

            IWebElement slderFrame = driver.FindElement(By.XPath("//iframe[@class='demo-frame']"));

            driver.SwitchTo().Frame(slderFrame);

            Thread.Sleep(2500);

            Actions action = new Actions(driver);

            // Offset 1 to Move Slider by 30 
            IWebElement slider = driver.FindElement(By.XPath("//*[@id='slider']/span[1]"));
            action.DragAndDropToOffset(slider, 30, 0).Perform();
            action.Release();
            Thread.Sleep(2500);

            // Offset 2 to Move Slider by 30 
            action = new Actions(driver);
            action.DragAndDropToOffset(slider, 30, 0).Perform();
            action.Release();
            Thread.Sleep(2500);

            // Offset 3 to Move Slider by 30 
            action = new Actions(driver);
            action.DragAndDropToOffset(slider, 30, 0).Perform();
            Thread.Sleep(2500);

            driver.Close();
            driver.Quit();
        }
    }
}

CodePudding user response:

Another Solution as below by creating Actions class object without reference object

            IWebElement slider = driver.FindElement(By.XPath("//*[@id='slider']/span[1]"));

            // Offset 1 to Move Slider by 30 
            new Actions(driver).DragAndDropToOffset(slider, sliderOffsetX, 0).Perform();
            Thread.Sleep(1000);

            // Offset 2 to Move Slider by 30 
            new Actions(driver).DragAndDropToOffset(slider, sliderOffsetX, 0).Perform();
            Thread.Sleep(1000);

            // Offset 3 to Move Slider by 30
            new Actions(driver).DragAndDropToOffset(slider, sliderOffsetX, 0).Perform();
            Thread.Sleep(1000);

    
  • Related