Python + Selenium Automated Test Life Restart Simulator

Introduction

Recently, there is a H5 game that is particularly popular, called "Life Restart Simulator". This game uses a black humorous form of ridicule to randomly generate a list of your life events. At the beginning, you can also extract talents and add points, which is full of fun.

Because of the excessive traffic on the official website, some friends have access to white screens and webpage failures. The editor is here to post an online link to the latest life restart simulator:

https://dushusir.com/life/

After restarting my life, I wondered if I could write a script to automate the running of this game. Because it is a pure web operation, I can use the automated test artifact combined with Python and Selenium to test it.

Analysis

Because this game is essentially a series of web page operations, it is relatively simple to implement, just as a review of the basic element operations of Selenium. Children's shoes who want to get started with Python can check it out.

I have previously written a Python+Selenium automated test synthesis big watermelon which can also be used for learning.

Note: This is not an intelligent script to improve game scores, but just a case study to demonstrate automated testing to help everyone better understand the usage of selenium.

Development Environment

  1. Download and install Python
  2. Install pip
  3. Install Selenium and browser webdriver

The mac platform is different from the windows platform. Pay attention to the environment when installing. The details are not listed here, just google it.

If you download Python or pip installation is slow, you can refer to this blog post to speed it up.

Install py module artifact

Code

After the environment is ready, copy the following code directly to the .py file, such as main.py, and execute python main.py can start webdriver to run the automated life restart simulator.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Google Chrome Driver
from selenium import webdriver


# sleep module, let the program stop running down
from time import sleep

# Import the random (random number) module
import random

# Load Google Chrome in mobile mode
mobile_emulation = {'deviceName':'iPhone 5'}
options = webdriver.ChromeOptions()
options.add_experimental_option("mobileEmulation", mobile_emulation)


driver = webdriver.Chrome(chrome_options=options)

# Set browser size
driver.set_window_rect(0,0,700,700)

sleep(1)
# Open the set URL
driver.get('https://dushusir.com/life')

# Implicit wait
driver.implicitly_wait(5)

# Click to reopen now
restart = driver.find_element_by_id('restart')
restart.click()

sleep(1)

# Click 10 consecutive draws
driver.find_element_by_id('random').click()

sleep(1)

# Random 1-10 numbers, used to draw talent cards

randomOne = random.randint(1,3)
randomTwo = random.randint(4,6)
randomThree = random.randint(7,10)

# Find the three talent card buttons and click
driver.find_element_by_css_selector("#talents li:nth-of-type("+ str(randomOne) +")").click()
driver.find_element_by_css_selector("#talents li:nth-of-type("+ str(randomTwo) +")").click()
driver.find_element_by_css_selector("#talents li:nth-of-type("+ str(randomThree) +")").click()

# Click to confirm: "Please select 3"
driver.find_element_by_css_selector("#next").click()

sleep(1)

# Click Random Assignment
driver.find_element_by_css_selector("#random").click()

sleep(2)

# Start a new life
driver.find_element_by_css_selector("#start").click()

# Continue to click on the life event list area until the content in the list area no longer increases, which means that life is over
isOver = False # end flag
life = 0 # The length of the previous life

while(not isOver):
    
    # Click to list life events
    driver.find_element_by_css_selector("#lifeTrajectory").click()
    print('click once'+str(life))

    sleep(1)

    # Note that you must use find_elements, not find_element, otherwise the len method will report an error
    li = driver.find_elements_by_css_selector("#lifeTrajectory li")

    currt_life = len(li)

    # If the list continues to increase, continue to click; once it does not increase, it means you are dead
    if currt_life> life:
        life = currt_life
    else:
        isOver = True
        print('end')

# Click on Life Summary
driver.find_element_by_id('summary').click()

sleep(5)
driver.quit()

Conclusion

The program is relatively sketchy, and many details are not perfect yet, but it can be used as a reference for small cases, and everyone is welcome to criticize and point out problems. There will be time to share more useful test scripts and fun games later.

Reference

Comments