测试雪球、等待页面某个元素加载完毕后,再进行点击、父类子类之间的继承
作者:互联网
test_other.py
测试的case所在的页面
from selenium import webdriver from time import sleep import json from selenium.webdriver import DesiredCapabilities from testXueqiu02.BasePage import BasePage from testXueqiu02.ProfilePage import ProfilePage '''先进行登录,再测试其他模块''' class Test_other(): def setup_class(self): self.driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME) self.driver.get("https://xueqiu.com/") self.driver.implicitly_wait(10) # 自选股票 def test_profilePage(self): print('jinru') driver = self.driver profile = ProfilePage(driver) profile.login() selectPage = profile.gotoSelectPage() selectPage.select("alibaba","09988") def teardown_class(self): sleep(10) self.driver.quit()
BasePage.py
父类,为了初始化driver,便于子类继承
from selenium.webdriver.remote.webdriver import WebDriver class BasePage(object): def __init__(self, driver): self.driver: WebDriver = driver
ProfilePage.py
业务模块所在类
from testXueqiu02.BasePage import BasePage from testXueqiu02.SelectPage import SelectPage import json class ProfilePage(BasePage): def login(self): '''加载文件中的cookie''' with open('./cookie.txt', 'r', encoding='utf8') as f: listCookies = json.loads(f.read()) '''浏览器加载cookie''' for cookie in listCookies: cookie_dict = { 'domain': '.xueqiu.com', 'name': cookie.get('name'), 'value': cookie.get('value'), "expires": '', 'path': '/', 'httpOnly': False, 'HostOnly': False, 'Secure': False } self.driver.add_cookie(cookie_dict) # 刷新浏览器 self.driver.refresh() #搜索模块 def gotoSelectPage(self): return SelectPage(driver)
等待页面某个元素加载完毕后,再进行点击
selectPage.py
股票搜索页面
from selenium.webdriver.common.by import By from testXueqiu02.BasePage import BasePage from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class SelectPage(BasePage):
#当页面的icon图标加载出来时,就认为页面加载完毕 def __init__(self,driver): self.driver = driver WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".optional_stocks .iconfont"))) def select(self,keyword,market): self.driver.find_element_by_css_selector(".optional__controls .search__dropdown input").send_keys(keyword) self.driver.find_element_by_xpath("//*[@class='search__dropdown__list']//li[contains(text(),'%s')]" %market).click()
以下是Java代码
package com.xueqiu; import org.testng.annotations.Test; import org.testng.annotations.BeforeClass; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; public class TestXueQiu { WebDriver driver = null; @Test public void f() throws Exception { ProfilePage pp = new ProfilePage(driver); pp.login(); SearchPage sp = pp.gotoSearchPage(); sp.select("alibaba", "09988"); } @BeforeClass public void beforeClass() throws Exception { System.setProperty("webdriver.chrome.driver", "D:\\python\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://xueqiu.com/"); Thread.sleep(5); } @AfterClass public void afterClass() throws Exception { Thread.sleep(2000); driver.quit(); } }
package com.xueqiu; import org.openqa.selenium.WebDriver; public class BasePage { public WebDriver driver = null; public BasePage() { } public BasePage(WebDriver driver) { this.driver = driver; } }
package com.xueqiu; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import com.google.gson.Gson; public class ProfilePage extends BasePage{ WebDriver driver; public ProfilePage() {} public ProfilePage(WebDriver driver) { this.driver = driver; } public void login() throws IOException { String path = System.getProperty("user.dir"); BufferedReader br = new BufferedReader(new FileReader(path+"\\src\\com\\xueqiu\\cookie2.txt")); String ss=null; String len; while((len=br.readLine())!=null) { ss = len; System.out.println(len); } br.close(); System.out.println(ss); Gson gson=new Gson(); MC mc=gson.fromJson(ss, MC.class); System.out.println(mc.getCookies().size()); for(int i=0;i<mc.getCookies().size();i++) { MyCookie myCookie = mc.getCookies().get(i); Cookie cookie = new Cookie(myCookie.getName(),myCookie.getValue(),myCookie.getDomain(),myCookie.getPath(),myCookie.getExpires(),false,false); driver.manage().addCookie(cookie); } driver.navigate().refresh(); } public SearchPage gotoSearchPage() { return new SearchPage(driver); } }
package com.xueqiu; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class SearchPage extends BasePage { WebDriver driver; public SearchPage() {} public SearchPage(WebDriver driver2) { this.driver = driver2; // 等待页面加载完毕,超时时间设为10秒 // (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { // // @Override // public Boolean apply(WebDriver driver) { // return driver.getTitle().contains("首页"); // } // // }); } public void select(String keyword,String market) throws InterruptedException { driver.findElement(By.cssSelector(".optional__controls .search__dropdown input")).sendKeys(keyword); Thread.sleep(3000); driver.findElement(By.xpath("//*[@class='search__dropdown__list']//li[contains(text(),'"+market+"')]")).click(); } }
标签:WebDriver,子类,self,driver,import,BasePage,父类,public,雪球 来源: https://www.cnblogs.com/ychun/p/14399209.html