[C#][Selenium] How to drag-hover-drop an element

后端 未结 1 1927
礼貌的吻别
礼貌的吻别 2021-01-16 15:37

The website I am running this against is on an internal server so I can\'t give a link but I can post some of the relevant code that is shown when you click \"show element\"

1条回答
  •  醉梦人生
    2021-01-16 16:19

    Selenium's actions for drag and drop do not play nice with HTML5 so I resorted to using jquery to drag the element.

    I used these as references: https://gist.github.com/rcorreia/2362544 http://elementalselenium.com/tips/39-drag-and-drop

    And this is the result:

    public void WhenAStudentIsMovedToANewGroup(string action, string student, string group)
        {
            WaitForAngular();
            IWebDriver driver = (IWebDriver)FeatureContext.Current["Driver"];
    
            /*
             * Load a version of jQuery that we can access
             */
            driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            js.ExecuteAsyncScript(loadJQuery, jQueryUrl);
    
            string dragEntity = string.Format("[title=\"{0}\"]", student);
            string target = string.Format("[ui-on-drop=\"model.onMoveDrop(group)\"]:contains({0})  ~ div > div .{1}", group, action);
    
    
            string javaScriptString = string.Format("{0}$('{1}').simulateDragDrop({{ dropTarget: '{2}'}});", dragAndDropHelper, dragEntity, target);
    
            //Execute the drag and drop against the HTML5
            js.ExecuteScript(javaScriptString);
    
        }
    
        const string dragAndDropHelper = @"(function( $ ) {
        $.fn.simulateDragDrop = function(options) {
                return this.each(function() {
                        new $.simulateDragDrop(this, options);
                });
        };
        $.simulateDragDrop = function(elem, options) {
                this.options = options;
                this.simulateEvent(elem, options);
        };
        $.extend($.simulateDragDrop.prototype, {
                simulateEvent: function(elem, options) {
                        /*Simulating drag start*/
                        var type = 'dragstart';
                        var event = this.createEvent(type);
                        this.dispatchEvent(elem, type, event);
    
                        /*Simulating drop*/
                        type = 'drop';
                        var dropEvent = this.createEvent(type, {});
                        dropEvent.dataTransfer = event.dataTransfer;
                        this.dispatchEvent($(options.dropTarget)[0], type, dropEvent);
    
                        /*Simulating drag end*/
                        type = 'dragend';
                        var dragEndEvent = this.createEvent(type, {});
                        dragEndEvent.dataTransfer = event.dataTransfer;
                        this.dispatchEvent(elem, type, dragEndEvent);
                },
                createEvent: function(type) {
                        var event = document.createEvent(""CustomEvent"");
                        event.initCustomEvent(type, true, true, null);
                        event.dataTransfer = {
                                data: {
                                },
                                setData: function(type, val){
                                        this.data[type] = val;
                                },
                                getData: function(type){
                                        return this.data[type];
                                }
                        };
                        return event;
                },
                dispatchEvent: function(elem, type, event) {
                        if(elem.dispatchEvent) {
                                elem.dispatchEvent(event);
                        }else if( elem.fireEvent ) {
                                elem.fireEvent(""on""+type, event);
                        }
                }
        });
    })(jQuery);";
    
    
    
        const string loadJQuery = @"(function(jqueryUrl, callback) {
    if (typeof jqueryUrl != 'string') {
        jqueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
    }
    if (typeof jQuery == 'undefined') {
        var script = document.createElement('script');
        var head = document.getElementsByTagName('head')[0];
        var done = false;
        script.onload = script.onreadystatechange = (function() {
            if (!done && (!this.readyState || this.readyState == 'loaded' 
                    || this.readyState == 'complete')) {
                done = true;
                script.onload = script.onreadystatechange = null;
                head.removeChild(script);
                callback();
            }
        });
        script.src = jqueryUrl;
        head.appendChild(script);
    }
    else {
        callback();
    }
    })(arguments[0], arguments[arguments.length - 1]);";
    

    0 讨论(0)
提交回复
热议问题