Watin can't find element when running outside main thread

111 Views Asked by At

The problem is with the button:contains('Deal') selector, the button containing deal does exist on the page and Watin can find it without problems when running in main thread.

    // Add bet, deal cards and wait for animation
    private void dealCards()
    {
        // Start the game if chip exists
        if (browser.Image(Find.BySrc(chipURL)).Exists)
        {
            browser.Image(Find.BySrc(chipURL)).Click();

            // This is where the thread stops

            browser.Button(Find.BySelector("button:contains('Deal')")).Click();
            Thread.Sleep(dealCardsAnimationTime);

            if (Convert.ToInt32(browser.Span(Find.ByClass("player-points")).Text) == 21)
            {
                consoleTextBox.AppendText("You won by blackjack.");
                gameOver = true;
            }

            return;
        }
    }

Update:

Browser is a Internet Explore window that I interact with using WatiN, other than the UI changes, the problem seems to be that WatiN can't find the button.

However it's still somewhat related to multithreading, as the same code works fine without multithreading and the element that it's searching for does exist.

Update 2: Changed title and description of problem as the old one appeared to be unrelated.

Context ( in case it's necessary )

    // When bet button is clicked
    private void button2_Click(object sender, EventArgs e)
    {

        blackjackThread = new Thread(blackjackGame);

        if (dealInProgress == false)
        {
            blackjackThread.Start();
            // blackjackGame();
        }
    }

    // Blackjack deal handler
    private void blackjackGame()
    {

        consoleTextBox.AppendText("\r\n");
        resetVariables();
        dealCards();

        if (gameOver == true)
        {
            checkResult();
            dealInProgress = false;
            blackjackThread.Abort();
            return;
        }

        checkCards();
        logPoints();

        while (gameOver == false)
        {
            botAction();
        }

        checkResult();
        dealInProgress = false;
        blackjackThread.Abort();
        return;
    }
0

There are 0 best solutions below