import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait

class TestMember:
    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '11.0'
        desired_caps['deviceName'] = 'c2e3cb90'
        desired_caps['appPackage'] = 'com.tencent.wework'
        desired_caps['appActivity'] = '.launch.LaunchSplashActivity'
        # 不清楚本地缓存，启动app
        desired_caps['noReset'] = 'true'
        desired_caps["ensureWebviewsHavePages"] = True
        # 设置页面等待空闲状态的时间为0秒
        desired_caps['settings[waitForIdleTimeout]'] = 1
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_member(self):
        name = '张三'
        phone = '13626859587'
        gender = '女'
        self.driver.find_element(MobileBy.XPATH,'//*[@text= "通讯录"]').click()
        self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,
                                 'new UiScrollable(new UiSelector().'
                                 'scrollable(true).instance(0)).'
                                 'scrollIntoView(new UiSelector().'
                                 'text("添加成员").instance(0));').click()
        self.driver.find_element(MobileBy.XPATH, "//*[@text='添加成员']").click()
        self.driver.find_element(MobileBy.XPATH,"//*[@text='手动输入添加']").click()
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='完整输入']").click()
        time.sleep(3)
        self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'姓名')]/../android.widget.EditText").send_keys(name)
        self.driver.find_element(MobileBy.XPATH,'//*[@text="男"]').click()
        if gender=='女':
            self.driver.find_element(MobileBy.XPATH,'//*[@text="女"]').click()
        else:
            self.driver.find_element(MobileBy.XPATH, '//*[@text="男"]').click()

        self.driver.find_element(MobileBy.ID,"com.tencent.wework:id/fwi").send_keys(phone)
        self.driver.find_element(MobileBy.XPATH,"//*[@text='保存']").click()
        ele = self.driver.find_element(MobileBy.XPATH, '//*[@class="android.widget.Toast"]').text
        assert ele =="添加成功"



