CasperJs and Jquery with chained Selects

前端 未结 2 2047
栀梦
栀梦 2020-12-06 23:41

I\'m trying to create a testing case for a web site which includes a form with 3 chained selects. The first select is populated by default when the web page is loaded. If an

2条回答
  •  一生所求
    2020-12-07 00:07

    Here is how I did it. Because the web context includes jQuery, we can use it to trigger events, and an important step is that we have to wait and validate after each ajax call before to process any next step.

    //set select values
    var optionFirstSelect  = 125;
    var optionSecondSelect = 6;    
    var optionThirdSelect  = 47; 
    
    //create casper object
    var casper = require('casper').create({
        loadImages:false,
        verbose: true,
        logLevel: 'debug'
    });
    
    //open url
    casper.start('http://thewebsite.com');
    
    casper.then(function(){
    
        //select option on first select
        this.evaluate(function(valueOptionSelect){
            $('select#s1').val(valueOptionSelect);
            $('select#s1').trigger('change');
        },optionFirstSelect);
    
        //wait until the second select is populated
        this.waitFor(function check() {
            return this.evaluate(function() {
                return document.querySelectorAll('select#s2 option').length > 1;
            });
        }, function then() {
    
                //select option on second select
                this.evaluate(function(valueOptionSelect){
                    $('select#s2').val(valueOptionSelect);
                    $('select#s2').trigger('change');
                },optionSecondSelect);
    
                //wait until the third select is populated        
                this.waitFor(function check() {
                    return this.evaluate(function() {
                        return document.querySelectorAll('select#s3 option').length > 1;
                    });
                }, function then() {
    
                        //select option on third select
                        this.evaluate(function(valueOptionSelect){
                            $('select#s3').val(valueOptionSelect);
                            $('select#s3').trigger('change');
                        },optionThirdSelect);
    
                        //wait until table with info is populated after an option is seleted on third select. 
                        this.waitFor(function check() {
                                    return this.evaluate(function() {
                                        return  document.querySelectorAll('table#info tbody tr').length > 1;
                                    });
                                }, function then() { 
    
                                //Do verifications here ...
                        });               
                });         
        }); 
    });
    
    casper.run(function() {
        //finish execution script 
        this.exit();
    });
    

提交回复
热议问题