Scraping IPO Results with Cypress

Scraping IPO Results with Cypress

What is?Cypress?

Cypress is a testing framework designed to make web testing accessible and enjoyable. It’s like a supercharged version of Selenium but more intuitive and user-friendly. Cypress allows developers to test their web applications seamlessly, ensuring everything works as expected.

Getting Started with?Cypress

To use Cypress, you need to install it in your project. It’s as simple as running a few commands in your terminal:

npm init -y
npm install cypress --save-dev        

Cypress is now ready to use in your project.

Environment Variables and?Login

Cypress allows you to use environment variables to securely store sensitive information like usernames and passwords. Create a cypress.env.json file in your project with the following content:

{
  "USERNAME": "your_username",
  "DP":"your_DP"
  "PASSWORD": "your_password",
  "IPONUMBER": 3 // Adjust as needed
}        

In your Cypress test, use these variables like this:

cy.login(Cypress.env('USERNAME'), Cypress.env('PASSWORD'));
        

Code Snippets and Explanations

Preparing for Each?Test

beforeEach(() => {
  cy.writeFile('cypress/fixtures/ipoData.txt', ''); // Clear data before each test run
});        

This snippet ensures that the data file (ipoData.txt) is clean before each test, avoiding any interference from previous runs.

Logging In and Extracting IPO?Results

it('IPO Results', () => {
  cy.visit('https://meroshare.cdsc.com.np/login');
cy.get('[class="selection"]').type(Cypress.env('DP')) //bank name
  cy.get('#username').type(Cypress.env('USERNAME'));
  cy.get('#password').type(Cypress.env('PASSWORD'));
  cy.get('[type="submit"]').click();        
cy.get('[class="nav-item"]').contains("Application Report").click()
         
        for(let i=0;i<Cypress.env("IPONUMBER");i++){
        cy.get('.company-list').then((report)=>{
          cy.wrap(report).eq(i).find('.btn-issue').click();
          cy.wait(2000)
          //save data
          cy.get('[tooltip="Company Name"]').invoke('text').as('ipoName');
          cy.get('[class="form-group"]').contains("Status").parent().parent().find('[class="input-group"]').invoke('text').as('ipoStatus');
           
      
          // Use alias to retrieve the values stored in the variables and write to the fixture
          cy.get('@ipoName').then((ipoName) => {
            // Log or use the IPO name as needed
            cy.log(`IPO Name: ${ipoName}`);
            cy.writeFile('cypress/fixtures/ipoData.txt', `IPO Name: ${ipoName.trim()}\n`, { flag: 'a+' }); // Append mode             
          });
      
          cy.get('@ipoStatus').then((ipoStatus) => {
            // Log or use the IPO status as needed
            cy.log(`IPO Status: ${ipoStatus}`);
            console.log(ipoStatus)
            if(ipoStatus.trim()=="Alloted"){
              summary[1][0]+=1;
              
            }else{
              summary[1][1]+=1;
              
            }
             
            cy.writeFile('cypress/fixtures/ipoData.txt', `IPO Status: ${ipoStatus.trim()}\n`, { flag: 'a+' }); // Append mode            
          });
      
          cy.go("back")
          cy.get('[class="nav-item"]').contains("Application Report").click()        
})
      }        

This test navigates to the MeroShare login page, enters the provided username and password, and then proceeds to extract IPO information.

Gathering Expiry?Date

cy.visit('https://meroshare.cdsc.com.np/#/ownProfile');
cy.get('[class="row info-box__expire-date"] [class="account-info__date"]').each((data, index) => {
  const text = data.text();
  cy.log(index);
  expiryDate[1][index] = text;
})        

This code snippet visits the user’s profile page to collect expiry dates related to IPO applications.

Summarizing and Writing?Results

cy.then(() => {
  cy.writeFile('cypress/fixtures/ipoData.txt', '\nSummary: \n' + table(summary), { flag: 'a+' }); // Append mode
});        
cy.then(() => {
  cy.writeFile('cypress/fixtures/ipoData.txt', '\n \nExpiry Date: \n' + table(expiryDate), { flag: 'a+' }); // Append mode
});        

These snippets create a summary table and an expiry date table, providing a clear overview of the IPO results and associated dates.

Explore the Code on?GitHub

The complete code is available in the GitHub repository: MeroShareAutomation

Wrapping Up

Automating the extraction of IPO results from MeroShare using Cypress is a beginner-friendly way to save time and streamline financial data handling. Remember to be mindful of legal and ethical considerations when web scraping. Additionally, always secure sensitive information, such as usernames and passwords, using proper mechanisms like environment variables.

Happy testing and automating your MeroShare experiences with Cypress!

Vijay Kumar Rana

Senior Software Engineer | PHP | Oracle Netsuite Developer | SuiteScript

1 å¹´

Awesome, Pratik

赞
回复

要查看或添加评论,请登录

Pratik Shrestha的更多文章

社区洞察

其他会员也浏览了