From d54cbf1f4a966daf965126582e2d99db519c13ae Mon Sep 17 00:00:00 2001 From: 20220044 <17777770044@qq.com> Date: Wed, 10 Jul 2024 11:27:13 +0800 Subject: [PATCH] ADD file via upload --- WebUITasks.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 WebUITasks.java diff --git a/WebUITasks.java b/WebUITasks.java new file mode 100644 index 0000000..342b5c7 --- /dev/null +++ b/WebUITasks.java @@ -0,0 +1,48 @@ +package com.hogwarts.base; + +import org.apache.log4j.Logger; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +public class WebUITasks { + private static int waitShot = 30; + private static Logger logger = Logger.getLogger(WebUITasks.class); + + public static void inputText(String txt, WebDriver driver) throws Exception{ + WebElement searchInput = findElementByXpath("//input[@name='wd' and @id='kw']",driver); + searchInput.sendKeys(txt); + logger.info("Search text: " + txt); + } + + public static void clickSearchBtn(WebDriver driver) throws Exception{ + WebElement searchBtn = findElementByXpath("//input[@type='submit' and @id='su']",driver); + searchBtn.click(); + logger.info("Click the search button."); + } + + private static WebElement findElementByXpath(String objXpath, WebDriver driver) throws Exception{ + WebElement wele = null; + long start = System.currentTimeMillis(); + long now = System.currentTimeMillis(); + while(((now - start) < waitShot * 1000) && (wele == null)){ + wait(1); + wele = driver.findElement(By.xpath(objXpath)); + now = System.currentTimeMillis(); + } + + if(wele == null){ + throw new Exception("Can not find out the element: " + objXpath); + } + + return wele; + } + + protected static void wait(int sec){ + try { + Thread.sleep(sec * 1000); + } catch (InterruptedException e) { + + } + } +} -- 2.34.1