Example #1
0
        public void WhenGetPostData_IfMultipleCheckboxesWithSameNamePresentAndChecked_ShouldBeIncludItInPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='checkbox' name='check1' checked />
                            <input type='text' name='input2' value='value2' />                            
                            <input type='checkbox' name='check1' checked />
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&check1=on&input2=value2&check1=on", postData.GetPostDataString());
            MSAssert.AreEqual(4, postData.Count);
        }
Example #2
0
        public void WhenGetPostData_IfSelectHasSelectedOptionWithoutValue_ShouldAddOptionsTextToPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <select name='select1'>
                                <option selected>option1text</option>
                            </select>
                            <input type='text' name='input1' value='value1' />
                            <input type='submit' name='submit1' value='submit1' />
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&submit1=submit1&select1=option1text", postData.GetPostDataString());
        }
Example #3
0
        public void IfFormContainsDifferentInputElements_PostDataCollection_ShouldBeAbleToFilter()
        {
            //Arrange
            string html = @"
                <html>
                        <body>
                            <form id='form1' action='action' method='put'>
                                <input type='text' name='textbox1' value='textvalue' />
                                <input type='password' name='password1' value='passvalue' />
                                <input type='checkbox' name='checkbox1' value='checkvalue' />
                                <input type='radio' name='radio1' value='radiovalue' />
                                <input type='reset' name='reset1' value='resetvalue' />
                                <input type='file' name='file1' value='filevalue' />
                                <input type='file' name='file2' value='filevalue2' />
                            </form>
                        </body>
                    </html>";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("textbox1=textvalue&password1=passvalue", postData.GetPostDataString(PostDataFieldType.Text));
            MSAssert.AreEqual(4, postData.Count);
            MSAssert.AreEqual(2, postData.FindAll(e => (e.Type == PostDataFieldType.File)).Count());
        }
Example #4
0
        public void BuildPostData_IfNoInputElementsExistReturnsEmpty()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.IsTrue(postData.Count == 0);
            MSAssert.IsTrue(string.IsNullOrEmpty(postData.GetPostDataString()));
        }
Example #5
0
        public void BuildPostData_ReturnsNameValuePairsForInputElements()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='text' name='input2' value='value2' />                            
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&input2=value2", postData.GetPostDataString());
            MSAssert.AreEqual(2, postData.Count);
        }
Example #6
0
        public void WhenGetPostData_IfSelectHasNoOptions_ShouldNotAddItToPostada()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <select name='select1' />
                            <input type='text' name='input1' value='value1' />
                            <input type='submit' name='submit1' value='submit1' />
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&submit1=submit1", postData.GetPostDataString());
        }
Example #7
0
        public void WhenGetPostData_IfMultipleSubmitButtonsAndNoSubmitId_ShouldReturnBothSubmitButtonData()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='submit' name='submit1' value='submit1' />
                            <input type='submit' name='submit2' value='submit2' />                            
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&submit1=submit1&submit2=submit2", postData.GetPostDataString());
        }
Example #8
0
        public void WhenGetPostData_IfMultipleSubmitButtonsWithSameName_ShouldOnlyReturnTheOneThatInitiatedThePost()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='submit' name='submitname' value='submit1' />
                            <input type='submit' name='submitname' value='submit2' />                            
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            var inputElement            = (HtmlInputElement)form.ChildElements.Find(new { value = "submit2" });
            PostDataCollection postData = form.GetPostDataCollection(inputElement);

            // Assert
            MSAssert.AreEqual("input1=value1&submitname=submit2", postData.GetPostDataString());
        }
Example #9
0
        public void WhenGetPostData_IfRadioPresentAndChecked_ShouldValueBeIncludItInPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='radio' name='check1' checked='checked' value='radiovalue' />
                            <input type='text' name='input2' value='value2' />                            
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&check1=radiovalue&input2=value2", postData.GetPostDataString());
            MSAssert.AreEqual(3, postData.Count);
        }
Example #10
0
        public void WhenGetPostData_IfElementIsDisabled_ShouldNotContainCheckboxInPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='checkbox' name='check1' checked disabled />
                            <input type='text' name='input2' value='value2' />                            
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&input2=value2", postData.GetPostDataString());
            MSAssert.AreEqual(2, postData.Count);
        }
Example #11
0
        /// <summary>
        /// Initializes and executes request and handles response.
        /// </summary>
        internal BrowserInfo ExecuteRequest(string url, string method, string contentType, PostDataCollection postData)
        {
            BrowserInfo browserInfo = new BrowserInfo();

            HttpWebRequest request = this._webRequestor.CreateRequest(url);

            request.Method = method.ToUpperInvariant();

            // Note: cookies should be set before ContentType (request reorganize headers and cookies after that)
            request.AddCookies(this._currentCookies);

            // Note: content type (and length) should be set before data sent to the request stream
            if (!string.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;
            }

            string requestData = "";

            if ((request.Method.Equals("post", StringComparison.OrdinalIgnoreCase) ||
                 request.Method.Equals("put", StringComparison.OrdinalIgnoreCase)) &&
                postData != null)
            {
                if (postData.HasFilesToUpload)
                {
                    //request.KeepAlive = true;
                    // Note: AddMultipartRequestData sets corresponding ContentType
                    requestData = request.AddMultipartRequestData(postData, this._fileSystem);
                }
                else
                {
                    requestData = request.AddStandardRequestData(postData.GetPostDataString());
                }
            }

            // fire event before request has been sent
            RequestSendingEventArgs args = new RequestSendingEventArgs(request, requestData);

            OnRequestSending(args);
            request = args.Request;

            HttpWebResponse response = null;

            try
            {
                // execute request
                response = this._webRequestor.ExecuteRequest(request);
            }
            catch (WebException e)
            {
                // We catch only WebException here. In this case there should be valid error page,
                // which we want to obtain from the Response. If there was different type of exception,
                // or we have another exception while reading response, then we let it go through.
                if (e.Response != null)
                {
                    response = (HttpWebResponse)e.Response;
                }
                else
                {
                    throw;
                }
            }

            // get raw body
            string body = GetStringFromStream(response.GetResponseStream(), Encoding.UTF8);

            // fire event after responce has been received
            OnResponseReceived(new ResponseReceivedEventArgs(response, body));

            if (!string.IsNullOrEmpty(body))
            {
                // store response body
                browserInfo.Data = "<BrowserEmulatorRoot>" + body + "</BrowserEmulatorRoot>";

                // remember current URL
                this._currentUri = response.ResponseUri;

                return(browserInfo);
            }

            throw new InvalidOperationException("Response did not contain html markup.");
        }
Example #12
0
        public void WhenGetPostData_IfSelectHasMultipleSelectedOptionDifferentSelected_ShouldAddAllOptionsValuesToPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <select name='select1'>
                                <option value='option1value' selected>option1text</option>
                                <option value='option2value' selected='true'>option2text</option>
                                <option value='option3value' selected='selected'>option3text</option>
                            </select>
                            <input type='text' name='input1' value='value1' />
                            <input type='submit' name='submit1' value='submit1' />
                        </div>
                    </form>
                </html>
                ";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("input1=value1&submit1=submit1&select1=option1value&select1=option2value&select1=option3value", postData.GetPostDataString());
        }
Example #13
0
        public void IfFormContainsUnknownElements_BuildPostData_ShouldContainOnlyKnownElements()
        {
            //Arrange
            string html = @"
                <html>
                        <body>
                            <form id='form1' action='action' method='put'>
                                <input type='text' name='textbox1' value='textvalue' />
                                <input type='password' name='password1' value='passvalue' />
                                <input type='checkbox' name='checkbox1' value='checkvalue' />
                                <input type='radio' name='radio1' value='radiovalue' />
                                <input type='reset' name='reset1' value='resetvalue' />
                                <input type='file' name='file1' value='filevalue' />
                                <input type='hidden' name='hidden1' value='hiddenvalue' />
                                <input type='submit' name='button1' value='button1' />
                                <input type='search' name='search1' value='search1' />
                                <input type='tel' name='tel1' value='tel1' />
                                <input type='url' name='url1' value='url1' />
                                <input type='email' name='email1' value='email1' />
                                <input type='datetime' name='datetime1' value='datetime1' />
                                <input type='date' name='date1' value='10/10/1981' />
                                <input type='month' name='month1' value='month1' />
                                <input type='week' name='week1' value='week1' />
                                <input type='time' name='time1' value='time1' />
                                <input type='number' name='number1' value='11' />
                            </form>
                        </body>
                    </html>";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("textbox1=textvalue&password1=passvalue&file1=filevalue&hidden1=hiddenvalue&button1=button1&search1=search1&tel1=tel1&url1=url1&email1=email1&datetime1=datetime1&date1=10%2f10%2f1981&month1=month1&week1=week1&time1=time1&number1=11", postData.GetPostDataString());
            MSAssert.AreEqual(15, postData.Count);
        }