I’m originally from Russia and since I was a kid I been playing Chess and Checkers. I have heard about competitions between humans and computers, and it was always interesting to me what is going to happen if computer play against computer. Since now I’m test automation engineer I have an idea how to implement such thing.

In this exercise we not going to automate whole game, instead we just going to make a move and give away one piece to computer. We going to use chromedriver and the website to play checkers. Lets start our python project, here is the structure we going to have:

demo_project
- tests
-- checkers_test.py
-- chromedriver

Now let’s add selenium to our project by running

pip install selenium

Now open checkers_test.py file and initialize driver

driver = webdriver.Chrome('/path_to_chromedriver/demo_project/tests/chromedriver')
    driver.get('https://www.gamesforthebrain.com/game/checkers/')

While inspecting dom elements of checkers board we can see that in order to make a move we have to select a peace and then click on the area where we want to move it here is how it is going to look like:

driver.find_element_by_name('space62').click()
driver.find_element_by_name('space73').click()

Once you make a move you have to wait for computer make a move against you. For the purpose of this exercise we not going to use Implicit and Explicit waits, let’s just set sleep function from python time library, now our code will like this:

driver = webdriver.Chrome('/path_to_chromedriver/demo_project/tests/chromedriver')   driver.get('https://www.gamesforthebrain.com/game/checkers/')
    driver.find_element_by_name('space62').click()
    driver.find_element_by_name('space73').click()
    sleep(2)

Now lets assert we getting the right bottom message (the one saying ‘Make a move’) and if it is our turn we going to give it away to our opponent

driver = webdriver.Chrome('/path_to_chromedriver/demo_project/tests/chromedriver')
    driver.get('https://www.gamesforthebrain.com/game/checkers/')
    driver.find_element_by_name('space62').click()
    driver.find_element_by_name('space73').click()
    sleep(2)
    message = driver.find_element_by_id('message').text
    if message == 'Make a move.':
        driver.find_element_by_name('space73').click()
        driver.find_element_by_name('space64').click()
    else:
      print(message)
    sleep(2)

So how do we know that it is actually no longer exist and opponent have got it, in selenium we can use get_attribute function and then we will need to assert the results, for this we going to use pytest, lets install it:

pip install pytest

And here how our final test going to look like:

from selenium import webdriver
from time import sleep


def test_ui():
driver = webdriver.Chrome('/path_to_chromedriver/demo_project/tests/chromedriver')
    driver.get('https://www.gamesforthebrain.com/game/checkers/')
    driver.find_element_by_name('space62').click()
    driver.find_element_by_name('space73').click()
    sleep(2)
    message = driver.find_element_by_id('message').text
    if message == 'Make a move.':
        driver.find_element_by_name('space73').click()
        driver.find_element_by_name('space64').click()
    else:
      print(message)
    sleep(2)

    missing_thing = driver.find_element_by_name('space64').get_attribute('src')
    assert str(missing_thing) is not 'you1.gif'

All is left is run the test and see the progress:

pytest checkers_test.py

Hope you got an idea how you can automate checkers game and with a little bit of logic we can actually get to point when program written by us beats computer.