Example #1
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 #2
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 #3
0
        public void WhenGetPostData_IfMultipleCheckboxesWithSameNamePresentAndCheckedWithValue_ShouldValueBeIncludItInPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='checkbox' name='check1' value='checkvalue1' checked />
                            <input type='text' name='input2' value='value2' />                            
                            <input type='checkbox' name='check1' value='checkvalue2' checked />
                        </div>
                    </form>
                </html>
                ";

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

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

            // Assert
            MSAssert.AreEqual("input1=value1&check1=checkvalue1&input2=value2&check1=checkvalue2", postData.GetPostDataString());
            MSAssert.AreEqual(4, postData.Count);
        }
Example #4
0
        public void WhenGetPostData_IfSelectHasSelectedOptionWithNonStdSymbols_ShouldEncodeValue()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <select name='select1'>
                                <option selected>option1<text</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=option1%3ctext", postData.GetPostDataString());
        }
Example #5
0
        public void WhenGetPostData_IfSelectHasNoSelectedOptions_ShouldNotAddItToPostada()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <select name='select1'>
                                <option value='option1value' />
                            </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", postData.GetPostDataString());
        }
Example #6
0
        public void Submit()
        {
            Logger.LogAction("Submitting " + GetType().Name + " '" + ToString() + "'");

            HtmlFormElement.submit();
            WaitForComplete();
        }
 public HtmlFormShowYoutubeVideo()
 {
     this.Title = "Show Youtube video";
     this.ElUri = this.AddElement(new ElementTextbox("videoId", "YouTube Video ID"));
     this.GetElement("videoId").setHelpText("The v=... that is in the YouTube video URL. eg: https://www.youtube.com/watch?v=<strong>r7d5XheWiBk</strong>");
     this.AddElement(new ElementCheckbox("autoplay", "Autoplay", "autoplay"));
     this.AddElement(new ElementNumber("start", "Seek to", 1));
     this.GetElement("start").setHelpText("The number of seconds from the start to begin playing the video");
     this.AddElementSubmit();
 }
        /// <summary>
        /// Executes a form submit
        /// </summary>
        private BrowserInfo ExecuteFormSubmit(HtmlFormElement form, HtmlInputElement submitInputElement = null)
        {
            Uri url = new Uri(this._emulator.CurrentUri, form.CachedAttributes.Get("action", this._emulator.CurrentUri));

            PostDataCollection postdataCollection = form.GetPostDataCollection(submitInputElement);

            // execute request
            return(PerformRequest(url.AbsoluteUri,                             // url
                                  form.CachedAttributes.Get("method", "post"), // method
                                  "application/x-www-form-urlencoded",         // contentType
                                  postdataCollection));                        // postData
        }
        /// <summary>
        /// Checks if source element is an input type=submit and handles the click if it is
        /// </summary>
        private bool TryHandleSubmitButtonClick(HtmlElement source, out BrowserInfo browserInfo)
        {
            browserInfo = null;
            HtmlInputElement button = source as HtmlInputElement;

            if (button != null && button.CachedAttributes.Type == HtmlInputElementType.Submit)
            {
                // retrieve the form
                HtmlFormElement form = button.FindParentForm();
                if (form == null)
                {
                    throw new InvalidOperationException(CLICKSUBMITBUTTON_FORMNOTFOUND);
                }

                browserInfo = ExecuteFormSubmit(form, button);
                return(true);
            }

            return(false);
        }
Example #10
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 #11
0
        internal void Post(HtmlFormElement form)
        {
            StringBuilder sb = new StringBuilder();

            foreach (HtmlElement ele in form.GetElementsByTagName("INPUT"))
            {
                if (ele is HtmlInputElement)
                {
                    HtmlInputElement input = (HtmlInputElement)ele;

                    if (0 == string.Compare(input.Type, "checkbox", true))
                    {
                        if (!input.Checked)
                        {
                            continue;
                        }
                    }
                    if (0 == string.Compare(input.Type, "radio", true))
                    {
                        if (!input.Checked)
                        {
                            continue;
                        }
                    }

                    if (sb.Length > 0)
                    {
                        sb.Append("&");
                    }
                    sb.Append(HttpUtility.UrlEncode(input.Name));
                    sb.Append("=");
                    sb.Append(HttpUtility.UrlEncode(input.Value));
                }
            }

            sb.Append("&x=10&y=10");

            Post(AbsolizeUrl(form.Action), sb.ToString());
        }
Example #12
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);
        }
Example #13
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 #14
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 #15
0
        public void WhenGetPostData_IfRadioPresentAndUnchecked_ShouldNotIncludeItInPostdata()
        {
            //Arrange
            string html = @"
                <html>
                    <form id='form1'>
                        <div>
                            <input type='text' name='input1' value='value1' />
                            <input type='radio' name='check1' />
                            <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 #16
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 #17
0
        // todo: rewrite and complete the stuff
        private async void OnFormSubmit(HtmlFormElement form, HtmlElement submitElement)
        {
            var method  = form.Method;
            var action  = form.Action;
            var enctype = form.Enctype;
            var target  = form.Target;

            if (submitElement is HtmlButtonElement button)
            {
                if (!string.IsNullOrEmpty(button.FormMethod))
                {
                    method = button.FormMethod;
                }

                if (!string.IsNullOrEmpty(button.FormAction))
                {
                    action = button.FormAction;
                }

                if (!string.IsNullOrEmpty(button.FormEnctype))
                {
                    enctype = button.FormEnctype;
                }

                if (!string.IsNullOrEmpty(button.FormTarget))
                {
                    target = button.FormTarget;
                }
            }

            if (string.IsNullOrEmpty(action))
            {
                return;
            }

            var dataElements = form.Elements.OfType <IFormElement>()
                               .Where(x =>
                                      !string.IsNullOrEmpty(x.Name) &&
                                      !(x is HtmlInputElement input && input.Type == "checkbox" && !input.Checked) &&   //skip unchecked checkboxes
                                      !x.Disabled);

            var replaceSpaces = method != "post" || enctype != "multipart/form-data";

            var data = string.Empty;

            if (method == "get")
            {
                data = string.Join("&", dataElements.Select(x =>
                                                            x.Name + "=" + (GetValue(x) is string strValue ? (replaceSpaces ? strValue.Replace(' ', '+') : strValue) : "")
                                                            ));

                if (enctype == "application/x-www-form-urlencoded")
                {
                    data = System.Uri.EscapeUriString(data);
                }
            }
            else if (method == "post")
            {
                if (enctype == "application/x-www-form-urlencoded")
                {
                    data = string.Join("&", dataElements.Select(x =>
                                                                x.Name + "=" + (GetValue(x) is string strValue ? WebUtility.UrlEncode(strValue).Replace("!", "%21") : "")
                                                                ));
                }
                else if (enctype == "text/plain")
                {
                    data = string.Concat(dataElements.Select(x => (x.Name + "=" + (GetValue(x) ?? "")) + "\r\n"));
                }
                else
                {
                    throw new NotImplementedException("multipart-form-data form encryption type is not supported");
                }
            }

            var isGet = method == "get";

            var url = isGet
                                ? action.Split('?')[0] + (string.IsNullOrEmpty(data) ? "" : "?" + data)
                                : action;

            if (action != "about:blank")
            {
                HtmlDocument document;

                HtmlIFrameElement targetFrame;
                if (!string.IsNullOrEmpty(form.Target) &&
                    (targetFrame = Document.GetElementsByName(target).FirstOrDefault() as HtmlIFrameElement) != null)
                {
                    document = targetFrame.ContentDocument = new HtmlDocument(Window);
                }
                else
                {
                    ResetDocument(true);
                    document = Document;
                }

                var request = CreateRequest(url);
                if (!isGet)
                {
                    //todo: use right encoding and enctype
                    request.Method = "POST";
                    request.Data   = Encoding.UTF8.GetBytes(data);
                    request.Headers["Content-Type"] = enctype;
                }

                var response = await ResourceProvider.SendRequestAsync(request);

                //what should we do if the frame is not found?
                if (response != null && (response.Type == null || response.Type.StartsWith(ResourceTypes.Html)))
                {
                    LoadFromResponse(document, response);
                }
            }
            //todo: handle 'about:blank'
        }
Example #18
0
 public void SetUp()
 {
     _document = new Document();
     _form     = (HtmlFormElement)_document.CreateElement("form");
 }
Example #19
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.imoveiscuritiba.com.br/resultados-busca/Venda/Residencial/Apartamento/Capital/Curitiba/400000-a-600000/0/34240288/javascript:selecionarNumeroExibicao(1000000000);";

            HtmlFormElement hfe = new HtmlFormElement();

            hfe.Method = "";
            hfe.ElementHtml();

            string URLTeste = "http://finance.yahoo.com/q/op?s=MSFT&m=2012-09";

            //CQ doc = CQ.CreateFromUrl(url);
            //the two tables have a class "yfnc_datamodoutline1", but wrap an inner table

            ///var rows = doc.Select(".proximo");
            //var row = rows[0].Cq();
            //var ja = jQuery(rows.FirstElement().FirstElementChild.ToString());
            //doc.AttrSet(rows.FirstElement().FirstElementChild.ToString(), true);
            //Assert.AreEqual(ja.Attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper.");
            //CsQuery.Tests.jQuery.Attribute atr = new CsQuery.Tests.jQuery.Attribute();


            //in CsQuery the indexer [] is sysnoymous with Select method
            //Each th header row has the class ".yfnc_tablehead1" - figure out which columsn to use
            //for the four parts you are interested in

            //var headers= rows.First().Find(".yfnc_tablehead1");
            //int strikeIndex = headers.Filter(":contains('Strike')").Index();
            //int symbolIndex = headers.Filter(":contains('Symbol')").Index();
            //int bidIndex = headers.Filter(":contains('Bid')").Index();
            //int askIndex = headers.Filter(":contains('Ask')").Index();

            //iterate over all rows, except the header one (the "has" excludes the header row)

            //foreach (var row in rows.Has("td")) {
            //    CQ cells = row.Cq().Find("td");

            //    string output = String.Format("Strike: {0} Symbol: {1} Bid: {2} ask: {3}",
            //        cells[strikeIndex].Cq().Text(),
            //        cells[symbolIndex].Cq().Text(),
            //        cells[bidIndex].Cq().Text(),
            //        cells[askIndex].Cq().Text());

            //    Response.Write(output+"<br />");
            //}



            // The HtmlWeb class is a utility class to get the HTML over HTTP
            HtmlWeb htmlWeb = new HtmlWeb();

            // Creates an HtmlDocument object from an URL
            HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(url);
            // Targets a specific node

            //document.LoadHtml("javascript:selecionarNumeroExibicao(1000000000);");
            HtmlNode someNode = document.GetElementbyId("");

            //Response.Write(someNode.ParentNode.InnerHtml);
            //HtmlNode someNode2 = document.GetElementbyId("paginacao");
            //IEnumerable<HtmlNode> allLinks2 = s//omeNode2.Descendants("a");
            //foreach (HtmlNode link2 in allLinks2)
            //{
            //    link2.CreateNavigator();
            //    // Checks whether the link contains an HREF attribute
            //    if (link2.Attributes.Contains("onclick"))
            //    {
            //        string teste = link2.Attributes["onclick"].Value;
            //        document.LoadHtml("http://www.imoveiscuritiba.com.br/scripts/resultados.js");
            //        document.CreateElement(teste);
            //        //document.LoadHtml(teste);
            //        //someNode = document.DocumentNode;
            //        break;
            //        //HtmlAgilityPack.HtmlDocument document2 = htmlWeb.Load(teste);
            //    }
            //}

            //someNode2 = document.CreateTextNode("onclick='javascript:pegarImoveis(2);'");
            someNode = document.DocumentNode;

            Response.Write(someNode.InnerHtml);

            // If there is no node with that Id, someNode will be null
            if (someNode == null)
            {
                // Extracts all links within that node
                IEnumerable <HtmlNode> allLinks = someNode.Descendants("a");
                int cont = 0;
                // Outputs the href for external links
                foreach (HtmlNode link2 in allLinks)
                {
                    link2.CreateNavigator();
                    // Checks whether the link contains an HREF attribute
                    if (link2.Attributes.Contains("href"))
                    {
                        string valor = "";
                        // Simple check: if the href begins with "http://", prints it out
                        valor = link2.Attributes["href"].Value;
                        if (valor.Contains("ficha-imovel/Venda"))
                        {
                            string minhaFrase = string.Format("{0} - {1}{2}", cont, valor, Environment.NewLine);
                            Response.Write("<a href='" + valor + "' target='_blank'>" + minhaFrase + "</a><br/>");
                            cont++;
                        }
                    }
                }
            }
        }
Example #20
0
        // todo: rewrite and complete the stuff
        private async void OnFormSubmit(HtmlFormElement form, HtmlElement submitElement)
        {
            var method  = form.Method;
            var action  = form.Action;
            var enctype = form.Enctype;
            var target  = form.Target;

            if (submitElement is HtmlButtonElement button)
            {
                if (!string.IsNullOrEmpty(button.FormMethod))
                {
                    method = button.FormMethod;
                }

                if (!string.IsNullOrEmpty(button.FormAction))
                {
                    action = button.FormAction;
                }

                if (!string.IsNullOrEmpty(button.FormEnctype))
                {
                    enctype = button.FormEnctype;
                }

                if (!string.IsNullOrEmpty(button.FormTarget))
                {
                    target = button.FormTarget;
                }
            }

            if (string.IsNullOrEmpty(action))
            {
                return;
            }

            var dataElements = form.Elements.OfType <IFormElement>().Where(x => !string.IsNullOrEmpty(x.Name));

            var replaceSpaces = method != "post" || enctype != "multipart/form-data";

            var data = string.Join("&", dataElements.Select(x =>
                                                            x.Name + "=" + (x.Value != null ? (replaceSpaces ? x.Value.Replace(' ', '+') : x.Value) : "")
                                                            ));

            if (enctype == "application/x-www-form-urlencoded")
            {
                data = System.Uri.EscapeUriString(data);
            }

            var isGet = method == "get";

            var url = isGet
                                ? action.Split('?')[0] + (string.IsNullOrEmpty(data) ? "" : "?" + data)
                                : action;

            if (action != "about:blank")
            {
                var document = new Document(Window);

                HtmlIFrameElement targetFrame;
                if (!string.IsNullOrEmpty(form.Target) &&
                    (targetFrame = Document.GetElementsByName(target).FirstOrDefault() as HtmlIFrameElement) != null)
                {
                    targetFrame.ContentDocument = document;
                }
                else
                {
                    ScriptExecutor.Clear();
                    Document = document;
                }

                var request = CreateRequest(url);
                if (!isGet)
                {
                    //todo: use right encoding and enctype
                    request.Method = "POST";
                    request.Data   = Encoding.UTF8.GetBytes(data);
                }

                var response = await ResourceProvider.SendRequestAsync(request);

                //what should we do if the frame is not found?
                if (response.Type.StartsWith(ResourceTypes.Html))
                {
                    LoadFromResponse(document, response);
                }
            }
            //todo: handle 'about:blank'
        }