예제 #1
0
 private WebElement findElement(OpenQA.Selenium.By by, int timeoutms, bool raise)
 {
     try {
         if (timeoutms < 1)
         {
             return(new WebElement(this._wd, _webElement.FindElement(by)));
         }
         return(new WebElement(this._wd, this._wd.WaitNoException(() => _webElement.FindElement(by), timeoutms)));
     } catch (Exception ex) {
         if (ex is NoSuchElementException || ex is TimeoutException)
         {
             if (raise)
             {
                 throw new Exception("Element not found. " + "Method=" + by.ToString().ToLower().Substring(3).Replace(": ", ", value="));
             }
             return(null);
         }
         throw;
     }
 }
        public bool ClickElementWithText(string tagName, string text, OpenQA.Selenium.IWebElement parentElement = null, int sleep = 0, string checkString = "||||||||||||||||")
        {
            var el = parentElement != null?parentElement.FindElement(By.XPath(string.Format(".//{0}[contains(text(),'{1}')]", tagName, text))) : Driver.FindElementByXPath(string.Format("//{0}[contains(text(),'{1}')]", tagName, text));

            if (el == null)
            {
                return(false);
            }
            el.Click();
            return(IsBrowserReady(sleep, checkString));
        }
        public IWebElement WaitForElementByName(string name, OpenQA.Selenium.IWebElement parentElement = null)
        {
            IWebElement el = null;
            DateTime    dt = DateTime.Now.AddMilliseconds(MaxRenderWait);

            while (DateTime.Now < dt)
            {
                try
                {
                    el = (parentElement != null) ? parentElement.FindElement(By.Name(name)) : Driver.FindElementByName(name);
                    if (el != null)
                    {
                        return(el);
                    }
                }
                catch (Exception e)
                {
                }
                Task.Delay(100).Wait();
            }
            return(Driver.FindElementByName(name));
        }
        public IWebElement WaitForElementWithText(string tagName, string text, OpenQA.Selenium.IWebElement parentElement = null)
        {
            IWebElement el = null;
            DateTime    dt = DateTime.Now.AddMilliseconds(MaxRenderWait);

            while (DateTime.Now < dt)
            {
                try
                {
                    el = (parentElement != null) ? parentElement.FindElement(By.XPath(string.Format(".//{0}[contains(text(),'{1}')]", tagName, text))) : Driver.FindElementByXPath(string.Format("//{0}[contains(text(),'{1}')]", tagName, text));

                    if (el != null)
                    {
                        return(el);
                    }
                }
                catch (Exception e)
                {
                }
                Task.Delay(100).Wait();
            }
            return(Driver.FindElementByXPath(string.Format("//{0}[contains(text(),'{1}')]", tagName, text)));
        }
        private E_ERROR_CODE loginAppsflyerSystem(string userName, string password)
        {
            string message         = string.Empty;
            string elementUserName = _AppsFlyerDataSpiderConfig.getElementUserName();
            string elementPassword = _AppsFlyerDataSpiderConfig.getElementPassword();

            QA.IWebElement searchNameElement               = null;
            QA.IWebElement searchPasswordElement           = null;
            QA.IWebElement submitButtonElement             = null;
            QA.IWebElement searchSubmitDivContainerElement = null;

            if ((elementUserName == null) || (elementPassword == null))
            {
                message = string.Format("Not conmfig element user name <{0}> or password <{1}>.", elementUserName, elementPassword);
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_NOT_CONFIG_ELEMENT_USER_NAME_OR_PASSWORD);
            }

            searchNameElement = FindElementByName(elementUserName);
            if (searchNameElement == null)
            {
                message = string.Format("Not found element <{0}>.", elementUserName);
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_NOT_FOUND_ELEMENT_USER_NAME);
            }

            searchPasswordElement = FindElementByName(elementPassword);
            if (searchPasswordElement == null)
            {
                message = string.Format("Not found element <{0}>.", elementPassword);
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_NOT_FOUND_ELEMENT_PASSWORD);
            }

            SendKeysToElement(searchNameElement, userName);
            SendKeysToElement(searchPasswordElement, password);

            //*[@id="login-form"]/div[6]/button
            searchSubmitDivContainerElement = FindElementByClassName("form-buttons");
            if (searchSubmitDivContainerElement == null)
            {
                message = "Not found element submit button container div.";
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_NOT_FOUND_FORM_BUTTONS_CONTAINER_ELEMENT);
            }

            submitButtonElement = searchSubmitDivContainerElement.FindElement(QA.By.TagName("button"));
            if (submitButtonElement == null)
            {
                message = "Not found element submit button.";
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_NOT_FOUND_ELEMENT_SUBMIT_BUTTON);
            }

            try
            {
                _WebDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(20));
                _WebDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(20));
                submitButtonElement.Submit();
            }
            catch (Exception ex)
            {
                message = string.Format("Submit search form faild, error message<{0}>,UA<{1}>.", ex.Message, _UserAgent);
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_LOGIN_FAILED);
            }

            if (!checkLoginSuccess(userName))
            {
                TakeScreenshot(string.Format("login_failed_{0}.png", CPublic.getDateString(DateTime.Now)));
                message = string.Format("Login failed,task<{0}>,keywords<{1}>.", userName, password);
                _SystemLog.writeLog2Console(LOG_LEVEL.ERR, message);
                return(E_ERROR_CODE.ERROR_LOGIN_FAILED);
            }
            _IsLoginSuccessed = true;
            return(E_ERROR_CODE.OK);
        }
 public IWebElement GetElement(OpenQA.Selenium.IWebElement sourceElement, string xpath = "..") // default parent
 {
     return(sourceElement.FindElement(By.XPath(xpath)));
 }