Handle Clipboard Paste in WPF

public YourWindow()
{
    InitializeComponent();

    // "yourTextBox" is your TextBox
    DataObject.AddPastingHandler(yourTextBox, OnPaste);
}

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
    var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
    if (!isText) return;

    var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
    // Manipulate the text here
    text = text.Replace("oldValue", "newValue"); // Example manipulation

    // Set the new data
    e.DataObject = new DataObject(DataFormats.UnicodeText, text);
}

Note that this event is triggered after the user initiates the paste command but before the content is actually pasted into the TextBox, allowing you to modify or cancel the paste operation.

References
https://stackoverflow.com/questions/3061475/paste-event-in-a-wpf-textbox

ASP.NET Authentication with Identity in a Web API with Bearer Tokens & Cookies in .NET 8

We’ll use the in-memory database for this example.

dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.InMemory
dotnet add package Swashbuckle.AspNetCore.Filters
class MyUser : IdentityUser {}
public class DataContext : IdentityDbContext<MyUser>
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {

    }
}

Program.cs

using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Filters;
using WebApplication1;
using WebApplication1.Data;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    options.AddSecurityDefinition("oauth2", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
    {
        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
        Name = "Authorization",
        Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey
    });

    options.OperationFilter<SecurityRequirementsOperationFilter>();
});


builder.Services.AddDbContext<DataContext>(options => options.UseInMemoryDatabase("AppDb"));
builder.Services.AddAuthentication();
builder.Services.AddIdentityApiEndpoints<MyUser>()
    .AddEntityFrameworkStores<DataContext>();


var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapIdentityApi<MyUser>();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

In Swagger

References
https://www.youtube.com/watch?v=8J3nuUegtL4
https://devblogs.microsoft.com/dotnet/whats-new-with-identity-in-dotnet-8/

Install .NET 8 on Ubuntu 22.04 using Microsoft package feed

Remove the existing .NET packages from your distribution. You want to start over and ensure that you don’t install them from the wrong repository.

sudo apt remove 'dotnet*' 'aspnet*' 'netstandard*'

Configure your package manager to ignore the .NET packages from the distribution’s repository. It’s possible that you’ve installed .NET from both repositories, so you want to choose one or the other.

touch /etc/apt/preferences
nano /etc/apt/preferences
Package: dotnet* aspnet* netstandard*
Pin: origin "<your-package-source>"
Pin-Priority: -10

Make sure to replace <your-package-source> with your distribution’s package source, for example, on Ubuntu you may use archive.ubuntu.com in the US.

Use the apt-cache policy command to find the source:

apt-cache policy '~ndotnet.*' | grep -v microsoft | grep '/ubuntu' | cut -d"/" -f3 | sort -u
# Get Ubuntu version
declare repo_version=$(if command -v lsb_release &> /dev/null; then lsb_release -r -s; else grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"'; fi)

# Download Microsoft signing key and repository
wget https://packages.microsoft.com/config/ubuntu/$repo_version/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

# Install Microsoft signing key and repository
sudo dpkg -i packages-microsoft-prod.deb

# Clean up
rm packages-microsoft-prod.deb

# Update packages
sudo apt update
sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-8.0

References
https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-2204
https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu
https://learn.microsoft.com/en-us/dotnet/core/install/linux-package-mixup?pivots=os-linux-redhat

Get and Set the value of an input element In Selenium using C#

Get the Value of an Input Element:

To retrieve the current value of an input element, you can use the GetAttribute method to fetch the “value” attribute. Here’s an example:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the Chrome driver
        IWebDriver driver = new ChromeDriver();

        // Navigate to your web page
        driver.Navigate().GoToUrl("your_website_url_here");

        // Find the input element by its ID (replace with your element's ID)
        IWebElement inputElement = driver.FindElement(By.Id("your_input_element_id"));

        // Get the current value of the input element
        string inputValue = inputElement.GetAttribute("value");

        // Print the value to the console
        Console.WriteLine("Current Value: " + inputValue);

        // Close the driver when done
        driver.Quit();
    }
}

Set the Value of an Input Element:

To set a new value to an input element, you can use the SendKeys method. Here’s an example:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the Chrome driver
        IWebDriver driver = new ChromeDriver();

        // Navigate to your web page
        driver.Navigate().GoToUrl("your_website_url_here");

        // Find the input element by its ID (replace with your element's ID)
        IWebElement inputElement = driver.FindElement(By.Id("your_input_element_id"));

        // Set a new value to the input element
        inputElement.SendKeys("New Value");

        // Close the driver when done
        driver.Quit();
    }
}

 

Send double-click event to a web element using Selenium in C#

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the Chrome driver
        IWebDriver driver = new ChromeDriver();

        // Navigate to your web page
        driver.Navigate().GoToUrl("your_website_url_here");

        // Find the element you want to double-click (replace with your element locator)
        IWebElement elementToDoubleClick = driver.FindElement(By.Id("your_element_id_here"));

        // Create an Actions object
        Actions actions = new Actions(driver);

        // Double-click the element
        actions.DoubleClick(elementToDoubleClick).Build().Perform();

        // You can add additional actions or interactions here if needed

        // Close the driver when done
        driver.Quit();
    }
}

 

Find td Element inside a tr Element using Selenium in C#

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the Chrome driver
        IWebDriver driver = new ChromeDriver();

        // Navigate to your web page
        driver.Navigate().GoToUrl("your_website_url_here");

        // Find the tr element by its class or other suitable attribute
        IWebElement trElement = driver.FindElement(By.ClassName("rgRow"));

        // Find the td element inside the tr element
        IWebElement tdElement = trElement.FindElement(By.TagName("td"));

        // You can now interact with the tdElement as needed
        // For example, to get its text:
        string tdText = tdElement.Text;

        // Close the driver when done
        driver.Quit();
    }
}

 

Clicking an element in Selenium WebDriver using C#

Clicking an element in Selenium WebDriver using C# is quite straightforward. Once you have located the element using any of the locator strategies (like ID, class name, XPath, etc.), you can use the Click method to perform a click action on that element.

Here’s a simple example to demonstrate how to find an HTML element by its XPath and then click it:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to a website
                driver.Navigate().GoToUrl("https://www.example.com");

                // Find the element by its XPath
                IWebElement element = driver.FindElement(By.XPath("your_xpath_here"));

                // Click the element
                element.Click();

                // Optionally, wait for the action to complete or for the next element to be visible
                // Example: using WebDriverWait
                
                // Close the driver
                driver.Quit();
            }
        }
    }
}

You can also use other locator strategies like By.Id, By.ClassName, etc., to find the element you want to click.

Finding an element by XPath in Selenium WebDriver using C#

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to a website
                driver.Navigate().GoToUrl("https://www.example.com");

                // Find element by its XPath
                IWebElement element = driver.FindElement(By.XPath("your_xpath_here"));

                // Perform actions on the element (e.g., click, send keys, etc.)
                string elementText = element.Text;

                // Output the text of the element
                Console.WriteLine($"Element text is: {elementText}");

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

 

Find an Element by Class Name and Text in Selenium

In Selenium WebDriver with C#, if you want to find an element based on both its class name and the text it contains, you can use XPath or CSS selectors for more advanced matching. XPath allows you to traverse the HTML DOM and perform more complex queries to find elements.

Here’s an example that demonstrates how to find an element by its class name and text content:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to a website
                driver.Navigate().GoToUrl("https://www.example.com");

                // Find element by its class name and text content using XPath
                IWebElement element = driver.FindElement(By.XPath("//*[contains(@class, 'element_class_name_here') and text()='element_text_here']"));

                // Perform actions on the element (e.g., click, send keys, etc.)
                // ... Your logic here ...

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

 

Find HTML Element by Class Name in Selenium

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to a website
                driver.Navigate().GoToUrl("https://www.example.com");

                // Find element by its class name
                IWebElement element = driver.FindElement(By.ClassName("element_class_name_here"));

                // Perform actions on the element (e.g., click, send keys, etc.)
                string elementText = element.Text;

                // Output the text of the element
                Console.WriteLine($"Element text is: {elementText}");

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