Python WEB automated testing practice, project scenario (detailed)

Contents: Guide

foreword

The proportion of manual testing has reached 70%. Compared with development, the threshold of testing is low, and the salary is relatively much lower than that of development, so automated testing is done.

On the one hand, it is to improve the depth of one's own technical level.
On the other hand, it is to solve the problem of lack of more efficient testing technology in China, and finally to improve its competitiveness in the market. It is impossible to get a high salary without keeping pace with the times.

Next, let's explain the file upload and pop-up window processing of the actual combat scene of the automated test.
I saw this video myself and it was pretty good
 

 

1. File upload

The input tag uses automatic upload, first locate the upload button, and then send_keys passes in the path as a value.

As shown in the figure, the enterprise WeChat file upload page locates the element information whose label is input and type is file, and then uses send_keys to pass in the file path as a value.

Python version:
 

driver.find_element(By.CSS_SELECTOR, "#js_upload_input").send_keys("./hogwarts.png")

Java version:

driver.findElement(By.cssSelector("#js_upload_input")).sendKeys("./hogwarts.png");
 

2. Pop-up window processing

During page operations, you may encounter alert, confirm, and prompt bullet boxes generated by JavaScript, which can be located by using the switch_to.alert() method.

Then use text, accept, dismiss, send_keys and other methods to operate.

switch_to.alert(): Get the alert box on the current page.
text: returns the text information in alert, confirm, prompt.
accept(): Accept the existing warning box, that is, click OK.
dismiss(): Dismiss the existing warning box, that is, click to cancel.

send_keys(keysToSend): Send text to the alert box. keysToSend: Send text to the alert box.

1. Alter pop-up box
Enter a piece of text and click the submit button, for example, a pop-up box will pop up to confirm the content. This scenario can be handled in the following way:

Python version:
 

"""Alert弹窗获取文本与确认操作"""
driver.get("http://xxxxx")
driver.find_element_by_name("b1").click()

# 添加显示等待,等待弹框的出现
WebDriverWait(driver, 5, 0.5).until(EC.alert_is_present())

# 切换到弹框
alert = driver.switch_to.alert

# 打印弹框的文本
print(alert.text)
#点击确定
alert.accept()
# 点击取消或者关闭弹框
# alert.dismiss()

Java version:

@Test
public void alertTest(){
    // Alert弹窗获取文本与确认操作
    driver.get("http:/xxxxx");
    driver.findElement(By.name("b1")).click();
    // 添加显示等待,等待弹框的出现
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.alertIsPresent());
    // 切换到弹框
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    //点击确定
    alert.accept();
    //点击取消或者关闭弹框
    // alert.dismiss();
}

2. Confirm bullet box

Python version:

"""Prompt 弹窗获取文本、输入内容、确认操作"""
driver.get("http://xxxxx")
driver.find_element_by_name("b1").click()

#添加显示等待,等待弹框的出现
WebDriverWait(driver, 5).until(EC.alert_is_present())
#切换到弹框
alert = driver.switch_to.alert
#向弹框输入一段文本
alert.send_keys('Selenium Alert弹出窗口输入信息')
#点击确定
alert.accept()

Java version:

@Test
public void alert1Test() {
    // Prompt 弹窗获取文本、输入内容、确认操作
    driver.get("http://xxxxx");
    driver.findElement(By.name("b1")).click();
    // 添加显示等待,等待弹框的出现
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.alertIsPresent());
    // 切换到弹框
    Alert alert = driver.switchTo().alert();
    // 向弹框输入一段文本
    alert.sendKeys("Selenium Alert弹出窗口输入信息");
    // 点击确定
    alert.accept();
}

3. prompt box

Python version:

"""Confirm弹窗获取文本、确认、取消操作"""
driver.get("http://xxxxx")
driver.find_element_by_name("b1").click()
# 等待弹出窗口出现
WebDriverWait(driver, 5).until(EC.alert_is_present())
#切换到弹框
alert = driver.switch_to.alert
#点击确定
alert.accept()
#点击取消
alert.dismiss()

Java version:

@Test
public void confirmTest() {
    // Confirm弹窗获取文本、确认、取消操作
    driver.get("http://sahitest.com/demo/confirmTest.htm");
    driver.findElement(By.name("b1")).click();
    // 添加显示等待,等待弹框的出现
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.alertIsPresent());
    // 切换到弹框
    Alert alert = driver.switchTo().alert();
    // 点击确定
    alert.accept();
    // 点击取消
    alert.dismiss();
}

3. Summary

You must be able to bend down to pick up the ears of wheat that others disdain, not blindly climb high, not blindly seek big things, and look for opportunities in small gaps; moreover, you must be able to raise your head, see the running direction clearly, and find the shortcut to surpass, so that Go fast on the right path.

There is no road longer than feet, no mountain higher than people, nothing impossible, only unexpected people. It is not the mountains and the sea that stop you from moving forward, but often a small grain of sand on the soles of your shoes!

You can't change the world, but you can change your concept; you can't change things, but you can change your mood; you can't change others' opinions, but you can change your own!
 

Guess you like

Origin blog.csdn.net/lzz718719/article/details/130714453