Tuesday, July 4, 2023

10 users at time in chrome

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Threading;

class Program
{
    static void Main()
    {
        // List of user credentials and content to post
        List<Dictionary<string, string>> users = new List<Dictionary<string, string>>
        {
            new Dictionary<string, string>
            {
                { "username", "user1@example.com" },
                { "password", "password1" },
                { "content", "Hello from User 1!" }
            },
            new Dictionary<string, string>
            {
                { "username", "user2@example.com" },
                { "password", "password2" },
                { "content", "Hello from User 2!" }
            },
            // Add more user credentials and content here
        };

        // Create threads for each user
        List<Thread> threads = new List<Thread>();
        foreach (var user in users)
        {
            Thread thread = new Thread(() => LoginAndPost(user["username"], user["password"], user["content"]));
            threads.Add(thread);
            thread.Start();
        }

        // Wait for all threads to finish
        foreach (var thread in threads)
        {
            thread.Join();
        }
    }

    // Function to log in and post content for each user
    static void LoginAndPost(string username, string password, string content)
    {
        // Initialize the WebDriver
        IWebDriver driver = new ChromeDriver(); // Change to the appropriate WebDriver for your browser

        // Navigate to Facebook
        driver.Navigate().GoToUrl("https://www.facebook.com");

        // Log in
        IWebElement emailInput = driver.FindElement(By.Id("email"));
        emailInput.SendKeys(username);
        IWebElement passwordInput = driver.FindElement(By.Id("pass"));
        passwordInput.SendKeys(password);
        passwordInput.SendKeys(Keys.Enter);

        // Wait for the News Feed page to load
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        wait.Until(driver => driver.Url.Contains("https://www.facebook.com/"));

        // Post content
        IWebElement postInput = driver.FindElement(By.XPath("//textarea[@name='xhpc_message_text']"));
        postInput.SendKeys(content);
        IWebElement postButton = driver.FindElement(By.XPath("//button[@data-testid='react-composer-post-button']"));
        postButton.Click();

        // Wait for the post to be published
        Thread.Sleep(3000);

        // Close the browser
        driver.Quit();
    }
}

No comments:

Post a Comment