单个测试脚本中的多次交互导致对操作进行总结

Posted

技术标签:

【中文标题】单个测试脚本中的多次交互导致对操作进行总结【英文标题】:Multiple Interaction in a single test script result into summing up the actions 【发布时间】:2021-12-07 12:31:25 【问题描述】:
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();
        
    

问题:上面的测试脚本中有三个交互

偏移量 1 -> *将滑块移动一次 30 *

偏移 2 -> 将滑块移动两次,每次 30(总共移动 60)

偏移量 3 -> 将滑块移动三次,每次移动 30(总共移动 90)

为什么要总结这些行动? 总结起来怎么保护??

【问题讨论】:

也许尝试在每个操作之前获取滑块元素。 @pcalkins :感谢您的提示,但它不起作用。 :) 但是在 Release 之后动作正常。 【参考方案1】:

下面的代码工作正常。每次执行后释放动作

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();
        
    

【讨论】:

【参考方案2】:

另一种解决方案:创建没有引用对象的Actions类对象:

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);

    

【讨论】:

以上是关于单个测试脚本中的多次交互导致对操作进行总结的主要内容,如果未能解决你的问题,请参考以下文章

postman测试沙箱

大开测试:性能-如何实现脚本分步录制(连载10)

大开测试:性能-如何实现脚本分步录制(连载10)

第1期Ubuntu入门篇:18shell脚本入门

jmeter实现一次登录,多次业务请求

高效深入linux系统实战总结