Ejemplo n.º 1
0
        public static void ShowCurrentQuestion()
        {
            try
            {
                //string currentAnswer = GetCorrectAnswerText(driver.PageSource);
                hq = new HamQuestion(driver.PageSource, HTOAuto.QuestionPool);
                string currentAnswer = hq.Answer;

                if (currentAnswer != string.Empty)
                {
                    // MessageBox.Show(currentAnswer);
                    MessageBox.Show(currentAnswer, hq.Question);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to find a question or answer on this page");
            }
        }
Ejemplo n.º 2
0
        public static IWebElement getAnswerElement()
        {
            string html = (string)driver.ExecuteScript("return document.documentElement.outerHTML");

            //string answer = GetCorrectAnswerText(html);

            hq = new HamQuestion(driver.PageSource, QuestionPool);
            Console.WriteLine("Question: " + hq.Question);

            string answer = hq.Answer;

            if (hq.Answer == string.Empty)
            {
                Console.WriteLine("getAnswerElement() has failed. Returning null");
                return(null);
            }


            //var originalanswer = answer;

            // use double qoute " in xpath unless the answer line contain  doublequotes in the answer
            string xPath = "//span[contains(text(), \"" + answer + "\") and contains(@class, 'unselectedAnswer')] ";

            // check for double quote in answer, if true, then use a single quote.
            if (answer.Contains('"'))
            {
                xPath = "//span[contains(text(), \'" + answer + "\') and contains(@class, 'unselectedAnswer')] ";
            }

            Console.WriteLine("Search Answer xPath: " + xPath);
            System.Collections.ObjectModel.ReadOnlyCollection <IWebElement> answerTextElement = null;
            try
            {
                answerTextElement = driver.FindElements(By.XPath(xPath));
                if (answerTextElement.Count == 0)
                {
                    answer = stringPreProcessor(answer);
                    xPath  = "//span[contains(text(), \"" + answer + "\") and contains(@class, 'unselectedAnswer')] ";
                    if (answer.Contains('"'))
                    {
                        xPath = "//span[contains(text(), \'" + answer + "\') and contains(@class, 'unselectedAnswer')] ";
                    }
                    answerTextElement = driver.FindElements(By.XPath(xPath));
                }
            }
            catch (Exception)
            {
                return(null);
            }


            //Console.WriteLine("");
            //Console.WriteLine("");
            //Console.WriteLine("");

            //  Console.WriteLine(answerTextElement.Count + " elements have been found containing: " + xPath);
            //  StringComparer(originalanswer, answer);
            foreach (var element in answerTextElement)
            {
                Console.WriteLine(element.Text.Trim());
                if (element.Text.Trim() == answer.Trim())
                {
                    return(element);
                }
            }
            return(null);
        }