??? 查找了Katalon文档,没有找到相关的关键字,所以考虑使用自定义关键字来达到PC界面模拟移动界面的操作。博主自己有2个不同的项目使用的是Katalon工具。第一个项目是微信小程序,只有移动界面,所以单独设置浏览器的尺寸就可以是移动界面。而第二个项目是PC、移动在一起的,所以单独设置浏览器尺寸是访问不到移动界面的,需要使用selenium,设置设备名称和user-agent。下面贴上两个关键字代码。
package newp
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable
import org.openqa.selenium.WebElement
import org.openqa.selenium.WebDriver
import org.openqa.selenium.By
import org.openqa.selenium.Dimension
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.chrome.ChromeDriver
import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.mobile.helper.MobileElementCommonHelper
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.exception.WebElementNotFoundException
public class changeWindowsSize {
@Keyword
def newwindowssize(int weight,int height,String murl) {
WebUI.openBrowser("")
WebDriver driver = DriverFactory.getWebDriver()
driver.manage().window().setSize(new Dimension(weight,height))
WebUI.navigateToUrl(murl)
}
@Keyword
def newwindowssize2(String murl) {
ChromeOptions options = new ChromeOptions()
options.addArguments("--window-size=390,844")//设置尺寸
options.addArguments('--user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"')//设置ua
HashMap<String,String> mobileEmulation= new HashMap<>()
mobileEmulation.put("devicName","iPhone 12 Pro")
options.setExperimentalOption("mobileEmulation",mobileEmulation)
WebDriver driver=new ChromeDriver(options)
driver.get(murl)
DriverFactory.changeWebDriver(driver)//把driver还给Katalon
}
}
????? 需要注意在Katalon中使用selenium操作之后,还需要把Driver还给Katalon,否则Katalon会提示找不到浏览器报错。