我正在使用 Selenium Python 撰寫一個自動化測驗,它將玩一個基于 Web 的井字游戲。方法 checkForWinner() 需要在每次點擊后檢查 UI 是否有一行顯示獲勝者的文本,但該方法沒有被呼叫,我不知道為什么。
import pytest
import time
import logging
from random import randint
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
LOGGER = logging.getLogger(__name__)
class Tags():
square1 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[1]"
square2 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[2]"
square3 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[3]"
square4 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[4]"
square5 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[5]"
square6 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[6]"
square7 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[7]"
square8 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[8]"
square9 = "(//div[contains(@class, 'board-row')]//button[contains(@class, 'square')])[9]"
resultOh = "//div[contains(@class, 'game-info')]//div[contains(text(), 'Winner: O')]"
resultEx = "//div[contains(@class, 'game-info')]//div[contains(text(), 'Winner: X')]"
resultTie = "//div[contains(@class, 'game-info')]//div[contains(text(), 'tie')]"
class TestCase_PlayTTT():
URL = "http://localhost:3000"
@pytest.fixture
def load_browser(self, browser):
browser.get(self.URL)
yield browser
def test_playTTT(self, load_browser):
squares = [Tags.square1,Tags.square2,Tags.square3,
Tags.square4,Tags.square5,Tags.square6,
Tags.square7,Tags.square8,Tags.square9]
clickedSquares = []
random_square = randint(0,8)
time.sleep(5)
if not clickedSquares:
LOGGER.debug("I made it into the first if statement")
element = load_browser.find_element(By.XPATH, squares[random_square])
element.click()
clickedSquares.append(random_square)
for i in clickedSquares:
LOGGER.debug("I made it into the for loop")
if i == random_square:
LOGGER.debug("I made it into the second if statement")
self.test_playTTT(load_browser)
else:
LOGGER.debug("I made it into the first else statement")
clickedSquares.append(random_square)
LOGGER.debug("I made it to the final warning")
element = load_browser.find_element(By.XPATH, squares[random_square])
element.click()
self.checkForWinner(load_browser)
def checkForWinner(self, load_browser):
if Tags.resultOh:
winner = 'O'
LOGGER.debug('Winner O')
assert winner
elif Tags.resultEx:
winner = 'X'
LOGGER.debug('Winner X')
assert winner
elif Tags.resultTie:
winner = 'None'
LOGGER.debug('Tie')
assert winner
else:
self.test_playTTT(load_browser)
每當運行腳本時,游戲都會得出結論并且腳本會掛起。游戲結束后瀏覽器應該關閉,但它沒有。它顯然在等待一個沒有被滿足的條件,但我看不到它是什么。
uj5u.com熱心網友回復:
我認為你被困在你的 for 回圈中,在那里你遞回地呼叫 test_PlayTTT() 方法。由于您正在與另一個機器人對戰,因此您需要在每個機器人移動后跟蹤游戲板的狀態。現在,看起來你只是在跟蹤這個機器人的動作,但它可能會點擊其他玩家已經占據的方格。您希望 clickedSquares 包含對方機器人點擊的方格,而不僅僅是這個方格。
在井字游戲中,每個玩家可以標記的最大方格數是 5,因此要么在每個玩家點擊一個方格后檢查獲勝者,要么在 clickedSquares 的大小達到 5 時呼叫 checkForWinner()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512223.html
下一篇:查找GOT父元素的子元素
