<commit_before>﻿using System;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using Joyride.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;

namespace Joyride.Platforms.Ios
{
    abstract public class IosScreen : Screen
    {
        protected static ScreenFactory ScreenFactory = new IosScreenFactory();

        public bool HasLabelContainingText(string labelText, int timeoutSecs = DefaultWaitSeconds)
        {
            var xpath = "//UIAStaticText[contains(@label,'" + labelText + "')]";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);
            return (element != null);
        }

        public virtual bool HasLabelContainingText(string collectionName, int index, string labelText, int timeoutSecs = DefaultWaitSeconds)
        {
            var element = GetElementInCollectionAt(collectionName, index);
            var texts = element.FindElements(By.ClassName("UIAStaticText"), timeoutSecs);

            if (texts.Count == 0)
                return false;

            var textWithLabelText = texts.FirstOrDefault(e => e.Text.Contains(labelText));
            return (textWithLabelText != null);
        }

        public void SetSlider(string elementName, int percentage)
        {
            if ((percentage < 0) || (percentage > 100))
                throw new IndexOutOfRangeException("Slider can only accept values 1-100.  Requested: " + percentage);

            var slider = FindElement(elementName);

            if (slider == null)
                throw new NoSuchElementException("Cannot find element:  " + elementName);

            var actualValue = (double)percentage / 100;
            slider.SendKeys(actualValue.ToString());
        }

        public int CurrentPageOnIndictator(string elementName)
        {
            var value = GetElementAttribute(elementName, "value");
            if (value == null)
                throw new NoSuchElementException("Unable to get value for element:  " + elementName);

            var match = Regex.Match(value, @"page (?<current>\d+) of (?<max>\d+)");
            if (match.Success)
                return Int32.Parse(match.Groups["current"].Value);

            throw new Exception("Unable to extra current page from:  " + value);
        }

        public bool HasNavigationBarTitled(string title, int timeoutSecs = DefaultWaitSeconds)
        {
            var xpath = "//UIANavigationBar[1]/UIAStaticText[@label='" + title + "']";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);
            return (element != null);
        }

        protected IWebElement FindModalDialog(string text = null, bool title = true, int timeoutSecs = 5)
        {
            const string xpath = "//UIAAlert[@visible='true']";

            if (text == null)
                return Driver.FindElement(By.XPath(xpath), timeoutSecs);

            string xpathContainsText;

            //TODO: For alerts the name attribute is actually populated but not the label.  
            if (title)
                xpathContainsText =
                    "//UIAAlert[@visible='true' and contains(@name, '" + text + "')]";
            else
                xpathContainsText = "//UIAAlert[@visible='true']//UIAStaticText[contains(@label, '" + text + "')]";

            return Driver.FindElement(By.XPath(xpathContainsText), timeoutSecs);
        }

        public virtual Screen AcceptModalDialog(bool accept = true, bool title = true, string alertText = null, int timeoutSecs = 5)
        {
            var dialog = FindModalDialog(alertText, title, timeoutSecs: timeoutSecs);

            if (dialog != null)
            {
                if (accept)
                    Driver.SwitchTo().Alert().Accept();
                else
                    Driver.SwitchTo().Alert().Dismiss();
            }
            return this;
        }

        public String TitleFromNavigationBar(int timeoutSecs = 5)
        {
            var xpath = "//UIANavigationBar[1]/UIAStaticText[1]";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);

            if (element == null)
                throw new NoSuchElementException("Unable to find the navigation bar with static text");

            return element.Text;
        }

        public bool HasModalDialog(string dialogText, bool title = true, int timeOutSecs = 5)
        {
            return (FindModalDialog(dialogText, title, timeOutSecs) != null);
        }


        public void HideKeyboard()
        {
            var windowSize = Driver.ScreenSize();
            var pointBehindKeyboard = new Point(windowSize.Width / 2, windowSize.Height / 3);
            Driver.Tap(pointBehindKeyboard);
        }<commit_msg>modal dialog handling is now moved out of iosscreen<commit_after>﻿using System;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using Joyride.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;

namespace Joyride.Platforms.Ios
{
    abstract public class IosScreen : Screen
    {
        protected static ScreenFactory ScreenFactory = new IosScreenFactory();

        public bool HasLabelContainingText(string labelText, int timeoutSecs = DefaultWaitSeconds)
        {
            var xpath = "//UIAStaticText[contains(@label,'" + labelText + "')]";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);
            return (element != null);
        }

        public virtual bool HasLabelContainingText(string collectionName, int index, string labelText, int timeoutSecs = DefaultWaitSeconds)
        {
            var element = GetElementInCollectionAt(collectionName, index);
            var texts = element.FindElements(By.ClassName("UIAStaticText"), timeoutSecs);

            if (texts.Count == 0)
                return false;

            var textWithLabelText = texts.FirstOrDefault(e => e.Text.Contains(labelText));
            return (textWithLabelText != null);
        }

        public void SetSlider(string elementName, int percentage)
        {
            if ((percentage < 0) || (percentage > 100))
                throw new IndexOutOfRangeException("Slider can only accept values 1-100.  Requested: " + percentage);

            var slider = FindElement(elementName);

            if (slider == null)
                throw new NoSuchElementException("Cannot find element:  " + elementName);

            var actualValue = (double)percentage / 100;
            slider.SendKeys(actualValue.ToString());
        }

        public int CurrentPageOnIndictator(string elementName)
        {
            var value = GetElementAttribute(elementName, "value");
            if (value == null)
                throw new NoSuchElementException("Unable to get value for element:  " + elementName);

            var match = Regex.Match(value, @"page (?<current>\d+) of (?<max>\d+)");
            if (match.Success)
                return Int32.Parse(match.Groups["current"].Value);

            throw new Exception("Unable to extra current page from:  " + value);
        }

        public bool HasNavigationBarTitled(string title, int timeoutSecs = DefaultWaitSeconds)
        {
            var xpath = "//UIANavigationBar[1]/UIAStaticText[@label='" + title + "']";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);
            return (element != null);
        }


        public String TitleFromNavigationBar(int timeoutSecs = 5)
        {
            var xpath = "//UIANavigationBar[1]/UIAStaticText[1]";
            var element = Driver.FindElement(By.XPath(xpath), timeoutSecs);

            if (element == null)
                throw new NoSuchElementException("Unable to find the navigation bar with static text");

            return element.Text;
        }


        public void HideKeyboard()
        {
            var windowSize = Driver.ScreenSize();
            var pointBehindKeyboard = new Point(windowSize.Width / 2, windowSize.Height / 3);
            Driver.Tap(pointBehindKeyboard);
        }
{'fraction_non_alphanumeric': 0.10063361908311591, 'fraction_numerical': 0.0053422785439184994, 'mean_word_length': 2.2604293236127986, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '327145', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2051, 'n_words': 534}