Exemplo n.º 1
0
 /// <summary>
 /// Sends a sequence of key strokes to the active element.
 /// </summary>
 /// <param name="keysOrModifiers">Modifier keys (Ctrl, Shit or Alt) or keys</param>
 /// <param name="keys">Keys</param>
 /// <returns>Self</returns>
 public Keyboard SendKeys(string keysOrModifiers, string keys = null)
 {
     if (keys != null)
     {
         keysOrModifiers = string.Concat(keysOrModifiers, keys, keysOrModifiers);
     }
     _session.Send(RequestMethod.POST, "/keys", "value", new string[] { keysOrModifiers });
     //_session.Send(RequestMethod.POST, "/keys", "value", keysOrModifiers.ToCharArray() );
     return(this);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Loads a web page in the current browser session. Same as Open method.
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="timeout">Optional timeout in milliseconds. Infinite=-1</param>
        /// <param name="raise">Optional - Raise an exception after the timeout when true</param>
        /// <returns>Return true if the url was openned within the timeout, false otherwise</returns>
        public bool Get(string url, int timeout = -1, bool raise = true)
        {
            if (_session == null)
            {
                this.Start();
            }
            RemoteSession session = _session;

            if (string.IsNullOrEmpty(url))
            {
                throw new Errors.ArgumentError("Argument 'url' cannot be null.");
            }

            if (timeout > 0)
            {
                session.timeouts.PageLoad = timeout;
                session.Send(RequestMethod.POST, "/timeouts", "type", "page load", "ms", timeout);
            }

            int idx = url.IndexOf("/");

            if (idx == 0)
            {
                //relative url
                if (_baseUrl == null)
                {
                    throw new Errors.ArgumentError("Base URL not defined. Define a base URL or use a full URL.");
                }
                url = string.Concat(_baseUrl, url);
            }
            else
            {
                //absolute url
                idx = url.IndexOf('/', idx + 3);
                if (idx != -1)
                {
                    _baseUrl = url.Substring(0, idx - 1);
                }
                else
                {
                    _baseUrl = url;
                }
            }

            try {
                session.Send(RequestMethod.POST, "/url", "url", url);
                return(true);
            } catch {
                if (raise)
                {
                    throw;
                }
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a web element matching the given method and value
        /// </summary>
        private void WaitAnyElementNotPresent(By byAny, int timeout)
        {
            RemoteSession session = this.session;
            string        uri     = this.uri + "/element";
            DateTime      endTime = session.GetEndTime(timeout);

            foreach (By by in (By[])byAny.Value)
            {
                if (by == null)
                {
                    break;
                }
                try {
                    string method = By.FormatStrategy(by.Strategy);
                    string value  = by.Value.ToString();
                    session.Send(RequestMethod.POST, uri, "using", method, "value", value);
                    while (true)
                    {
                        if (DateTime.UtcNow > endTime)
                        {
                            throw new Errors.ElementPresentError(byAny);
                        }
                        SysWaiter.Wait();
                        session.SendAgain();
                    }
                } catch (Errors.NoSuchElementError) { }
            }
        }
Exemplo n.º 4
0
        private WebElements FindAnyElements(By byAny, int minimum, int timeout)
        {
            RemoteSession session     = this.session;
            string        uri         = this.uri + "/elements";
            WebElements   webelements = new WebElements();
            DateTime      endTime     = session.GetEndTime(timeout);

            while (true)
            {
                foreach (By by in (By[])byAny.Value)
                {
                    if (by == null)
                    {
                        break;
                    }
                    var  method   = By.FormatStrategy(by.Strategy);
                    var  value    = (string)by.Value;
                    List elements = (List)session.Send(RequestMethod.POST, uri, "using", method, "value", value);
                    webelements.Add(session, elements);
                }
                if (webelements.Count >= minimum)
                {
                    return(webelements);
                }
                if (DateTime.UtcNow > endTime)
                {
                    throw new Errors.NoSuchElementError(byAny);
                }
                SysWaiter.Wait();
            }
        }
Exemplo n.º 5
0
        private WebElement FindAnyElement(By byAny, int timeout)
        {
            RemoteSession session     = this.session;
            string        relativeUri = this.uri + "/element";
            Dictionary    element;
            DateTime      endTime = session.GetEndTime(timeout);

            while (true)
            {
                foreach (By by in (By[])byAny.Value)
                {
                    if (by == null)
                    {
                        break;
                    }
                    try {
                        string method = By.FormatStrategy(by.Strategy);
                        string value  = by.Value.ToString();
                        element = (Dictionary)session.Send(RequestMethod.POST, relativeUri, "using", method, "value", value);
                        return(new WebElement(session, element));
                    } catch (Errors.NoSuchElementError) { }
                }
                if (DateTime.UtcNow > endTime)
                {
                    throw new Errors.NoSuchElementError(byAny);
                }
                SysWaiter.Wait();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a new cookie.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="domain"></param>
        /// <param name="expiry"></param>
        /// <param name="secure"></param>
        /// <param name="httpOnly"></param>
        internal static void AddOne(RemoteSession session, string name, string value, string path, string domain, string expiry, bool secure, bool httpOnly)
        {
            var dict = new Dictionary();

            dict.Add("name", name);
            dict.Add("value", value);
            if (path != null)
            {
                dict.Add("path", path);
            }
            if (domain != null)
            {
                dict.Add("domain", domain);
            }
            if (expiry != null)
            {
                var dt = (long)(DateTime.Parse(expiry) - Cookie.BASE_TIME).TotalSeconds;
                dict.Add("expiry", dt);
            }
            if (secure == true)
            {
                dict.Add("secure", true);
            }
            if (httpOnly == true)
            {
                dict.Add("httpOnly", true);
            }
            session.Send(RequestMethod.POST, "/cookie", "cookie", dict);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns a web element matching the given method and value or null if no element found
        /// </summary>
        private WebElement FindFirstElement(Strategy strategy, string value, int timeout)
        {
            RemoteSession session     = this.session;
            string        relativeUri = this.uri + "/element";
            Dictionary    element;

            try {
                string method = By.FormatStrategy(strategy);
                element = (Dictionary)session.Send(RequestMethod.POST, relativeUri, "using", method, "value", value);
            } catch (Errors.NoSuchElementError) {
                if (timeout == 0)
                {
                    throw;
                }
                var endTime = session.GetEndTime(timeout);
                while (true)
                {
                    SysWaiter.Wait();
                    try {
                        element = (Dictionary)session.SendAgain();
                        break;
                    } catch (Errors.NoSuchElementError) {
                        if (DateTime.UtcNow > endTime)
                        {
                            throw;
                        }
                    }
                }
            }
            return(new WebElement(session, element));
        }
Exemplo n.º 8
0
        internal static Alert SwitchToAlert(RemoteSession session, int timeout)
        {
            string text;

            try {
                text = (string)session.Send(RequestMethod.GET, "/alert_text");
            } catch (Errors.NoAlertPresentError) {
                if (timeout == 0)
                {
                    throw;
                }
                DateTime endTime = session.GetEndTime(timeout);
                while (true)
                {
                    SysWaiter.Wait();
                    try {
                        text = (string)session.SendAgain();
                        break;
                    } catch (Errors.NoAlertPresentError) {
                        if (DateTime.UtcNow > endTime)
                        {
                            throw;
                        }
                    }
                }
            }
            return(new Alert(session, text));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Get or set the storage item for the given key.
 /// </summary>
 /// <param name="key">The key to set.</param>
 /// <param name="value">The value to set.</param>
 /// <returns>object</returns>
 public string this[string key] {
     get {
         return((string)_session.Send(RequestMethod.GET, _uri + "/key/" + key));
     }
     set {
         this.Set(key, value);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Returns all the web elements matching the given method and value
        /// </summary>
        private WebElements FindAllElements(Strategy strategy, string value, int minimum, int timeout)
        {
            RemoteSession session = this.session;
            string        uri     = this.uri + "/elements";

            try {
                var  method   = By.FormatStrategy(strategy);
                List elements = session.SendUntil(timeout,
                                                  () => (List)session.Send(RequestMethod.POST, uri, "using", method, "value", value),
                                                  (r) => r.Count >= minimum
                                                  );
                return(new WebElements(session, elements));
            } catch (Errors.TimeoutError) {
                throw new Errors.NoSuchElementError(strategy, value);
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Adds a new cookie.
 /// </summary>
 /// <param name="session"></param>
 /// <param name="name"></param>
 /// <param name="value"></param>
 /// <param name="path"></param>
 /// <param name="domain"></param>
 /// <param name="expiry"></param>
 /// <param name="secure"></param>
 /// <param name="httpOnly"></param>
 internal static void AddOne(RemoteSession session, string name, string value, string path, string domain, string expiry, bool secure, bool httpOnly) {
     var dict = new Dictionary();
     dict.Add("name", name);
     dict.Add("value", value);
     if (path != null)
         dict.Add("path", path);
     if (domain != null)
         dict.Add("domain", domain);
     if (expiry != null) {
         var dt = (long)(DateTime.Parse(expiry) - Cookie.BASE_TIME).TotalSeconds;
         dict.Add("expiry", dt);
     }
     if (secure == true)
         dict.Add("secure", true);
     if (httpOnly == true)
         dict.Add("httpOnly", true);
     session.Send(RequestMethod.POST, "/cookie", "cookie", dict);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Returns a web element matching the given method and value
        /// </summary>
        private void WaitElementNotPresent(Strategy strategy, string value, int timeout)
        {
            RemoteSession session = this.session;
            string        uri     = this.uri + "/element";
            string        method  = By.FormatStrategy(strategy);
            DateTime      endTime = session.GetEndTime(timeout);

            try {
                session.Send(RequestMethod.POST, uri, "using", method, "value", value);
                while (true)
                {
                    SysWaiter.Wait();
                    session.SendAgain();
                    if (DateTime.UtcNow > endTime)
                    {
                        throw new Errors.ElementPresentError(strategy, value);
                    }
                }
            } catch (Errors.NoSuchElementError) { }
        }
Exemplo n.º 13
0
 internal static Alert SwitchToAlert(RemoteSession session, int timeout) {
     string text;
     try {
         text = (string)session.Send(RequestMethod.GET, "/alert_text");
     } catch (Errors.NoAlertPresentError) {
         if (timeout == 0)
             throw;
         DateTime endTime = session.GetEndTime(timeout);
         while (true) {
             SysWaiter.Wait();
             try {
                 text = (string)session.SendAgain();
                 break;
             } catch (Errors.NoAlertPresentError) {
                 if (DateTime.UtcNow > endTime)
                     throw;
             }
         }
     }
     return new Alert(session, text);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Returns the element with focus, or BODY if nothing has focus.
        /// </summary>
        internal static WebElement GetActiveWebElement(RemoteSession session)
        {
            var result = (Dictionary)session.Send(RequestMethod.POST, "/element/active");

            return(new WebElement(session, result));
        }
Exemplo n.º 15
0
 internal static string GetWindowHandle(RemoteSession session) {
     return (string)session.Send(RequestMethod.GET, "/window_handle");
 }
Exemplo n.º 16
0
 /// <summary>
 /// Delete the cookie matching the name on the current page.
 /// </summary>
 /// <returns>object</returns>
 internal static void DeleteByName(RemoteSession session, string name)
 {
     session.Send(RequestMethod.DELETE, "/cookie/" + name);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Delete the cookie matching the name on the current page.
 /// </summary>
 /// <returns>object</returns>
 internal static void DeleteByName(RemoteSession session, string name) {
     session.Send(RequestMethod.DELETE, "/cookie/" + name);
 }
Exemplo n.º 18
0
 internal static List GetWindowsHandles(RemoteSession session) {
     return (List)session.Send(RequestMethod.GET, "/window_handles");
 }
Exemplo n.º 19
0
 /// <summary>
 /// Move the mouse to the specificed element.
 /// </summary>
 /// <param name="element">Opaque ID assigned to the element to move to, as described in the WebElement JSON Object. If not specified or is null, the offset is relative to current position of the mouse.</param>
 public Mouse moveTo(WebElement element)
 {
     _session.Send(RequestMethod.POST, "/moveto", "element", element.Id);
     return(this);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Delete all cookies visible to the current page.
 /// </summary>
 internal static void DeleteAll(RemoteSession session) {
     session.Send(RequestMethod.DELETE, "/cookie");
 }
Exemplo n.º 21
0
 /// <summary>
 /// Dismisses the alert.
 /// </summary>
 public void Dismiss()
 {
     _session.Send(RequestMethod.POST, "/dismiss_alert");
 }
Exemplo n.º 22
0
 private static void SendTimeoutPageLoad(RemoteSession session, int timeout) {
     session.Send(RequestMethod.POST, "/timeouts", "type", "page load", "ms", timeout);
 }
Exemplo n.º 23
0
 private static void SendTimeoutImplicit(RemoteSession session, int timeout) {
     session.Send(RequestMethod.POST, "/timeouts", "type", "implicit", "ms", timeout);
 }
Exemplo n.º 24
0
 internal static string GetCurrentTitle(RemoteSession session) {
     return (string)session.Send(RequestMethod.GET, "/title");
 }
Exemplo n.º 25
0
 internal static string ActivateWindow(RemoteSession session, string name) {
     session.Send(RequestMethod.POST, "/window", "name", name);
     return (string)session.Send(RequestMethod.GET, "/window_handle");
 }
Exemplo n.º 26
0
 private object Send(RequestMethod method, string relativeUri)
 {
     return(_session.Send(method, this.uri + relativeUri));
 }
Exemplo n.º 27
0
 private static void SendTimeoutImplicit(RemoteSession session, int timeout)
 {
     session.Send(RequestMethod.POST, "/timeouts", "type", "implicit", "ms", timeout);
 }
Exemplo n.º 28
0
 /// <summary>
 /// Get all cookies visible to the current page.
 /// </summary>
 /// <param name="session"></param>
 /// <returns></returns>
 internal static List GetAll(RemoteSession session)
 {
     return((List)session.Send(RequestMethod.GET, "/cookie"));
 }
Exemplo n.º 29
0
        /// <summary>
        /// Gets the position of the browser window relative to the upper-left corner of the screen.
        /// </summary>
        /// <remarks>When setting this property, it should act as the JavaScript window.moveTo() method.</remarks>
        public Point Position()
        {
            var result = (Dictionary)_session.Send(RequestMethod.GET, uri() + "/position");

            return(new Point(result));
        }
Exemplo n.º 30
0
 /// <summary>
 /// Delete all cookies visible to the current page.
 /// </summary>
 internal static void DeleteAll(RemoteSession session)
 {
     session.Send(RequestMethod.DELETE, "/cookie");
 }
Exemplo n.º 31
0
 internal static string GetWindowHandle(RemoteSession session)
 {
     return((string)session.Send(RequestMethod.GET, "/window_handle"));
 }
Exemplo n.º 32
0
        /*
         * public IME IME {
         *  get {
         *      return _ime ?? (_ime = new IME(_session));
         *  }
         * }
         */


        #region Location

        /// <summary>
        /// Get the current geo location.
        /// </summary>
        /// <returns>The current geo location. {latitude: number, longitude: number, altitude: number} </returns>
        public Dictionary Location()
        {
            return((Dictionary)_session.Send(RequestMethod.GET, "/location"));
        }
Exemplo n.º 33
0
 /// <summary>
 /// Get all cookies visible to the current page.
 /// </summary>
 /// <param name="session"></param>
 /// <returns></returns>
 internal static List GetAll(RemoteSession session) {
     return (List)session.Send(RequestMethod.GET, "/cookie");
 }
Exemplo n.º 34
0
        /// <summary>
        /// Returns true if the screen is portrait, false otherwise.
        /// </summary>
        /// <returns></returns>
        public bool IsPortrait()
        {
            var value = (string)_session.Send(RequestMethod.GET, "/orientation");

            return("PORTRAIT".Equals(value));
        }
Exemplo n.º 35
0
 private static void SendTimeoutPageLoad(RemoteSession session, int timeout)
 {
     session.Send(RequestMethod.POST, "/timeouts", "type", "page load", "ms", timeout);
 }